Skip to content

Commit

Permalink
Less string buffers and less string copies
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Jan 16, 2025
1 parent 86c9a43 commit 0b32901
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 149 deletions.
21 changes: 6 additions & 15 deletions cheat_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ bool cheat_manager_get_code_state(unsigned i)
return cheat_st->cheats[i].state;
}

static bool cheat_manager_get_game_specific_filename(
static size_t cheat_manager_get_game_specific_filename(
char *s, size_t len,
const char *path_cheat_database,
bool saving)
Expand All @@ -714,32 +714,23 @@ static bool cheat_manager_get_game_specific_filename(
runloop_state_t *runloop_st = runloop_state_get_ptr();
const char *core_name = NULL;
const char *game_name = NULL;

if (!core_get_system_info(&sysinfo))
return false;

return 0;
core_name = sysinfo.library_name;
game_name = path_basename_nocompression(runloop_st->name.cheatfile);

if ( string_is_empty(path_cheat_database)
|| string_is_empty(core_name)
|| string_is_empty(game_name))
return false;

fill_pathname_join_special(s1,
path_cheat_database, core_name,
return 0;
fill_pathname_join_special(s1, path_cheat_database, core_name,
sizeof(s1));

if (saving)
{
/* Check if directory is valid, if not, create it */
if (!path_is_valid(s1))
path_mkdir(s1);
}

fill_pathname_join_special(s, s1, game_name, len);

return true;
return fill_pathname_join_special(s, s1, game_name, len);
}

void cheat_manager_load_game_specific_cheats(const char *path_cheat_database)
Expand Down Expand Up @@ -789,7 +780,7 @@ int cheat_manager_initialize_memory(rarch_setting_t *setting, size_t idx, bool w
bool is_search_initialization = (setting != NULL);
rarch_system_info_t *sys_info = &runloop_state_get_ptr()->system;
unsigned offset = 0;
cheat_manager_t *cheat_st = &cheat_manager_state;
cheat_manager_t *cheat_st = &cheat_manager_state;
#ifdef HAVE_MENU
struct menu_state *menu_st = menu_state_get_ptr();
#endif
Expand Down
84 changes: 40 additions & 44 deletions command.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
#include "version.h"
#include "version_git.h"

#define CMD_BUF_SIZE 4096
#define CMD_BUF_SIZE 4096

static void command_post_state_loaded(void)
{
Expand Down Expand Up @@ -160,8 +160,7 @@ static void command_parse_sub_msg(command_t *handle, const char *tok)
RARCH_WARN(msg_hash_to_str(MSG_UNRECOGNIZED_COMMAND), tok);
}

static void command_parse_msg(
command_t *handle, char *buf)
static void command_parse_msg(command_t *handle, char *buf)
{
char *save = NULL;
const char *tok = strtok_r(buf, "\n", &save);
Expand Down Expand Up @@ -235,8 +234,8 @@ command_t* command_network_new(uint16_t port)
command_t *cmd = (command_t*)calloc(1, sizeof(*cmd));
command_network_t *netcmd = (command_network_t*)calloc(
1, sizeof(command_network_t));
int fd = socket_init(
(void**)&res, port, NULL, SOCKET_TYPE_DATAGRAM, AF_INET);
int fd = socket_init((void**)&res, port, NULL,
SOCKET_TYPE_DATAGRAM, AF_INET);

RARCH_LOG("[NetCMD]: %s %hu.\n",
msg_hash_to_str(MSG_BRINGING_UP_COMMAND_INTERFACE_ON_PORT),
Expand Down Expand Up @@ -1255,25 +1254,22 @@ bool command_event_resize_windowed_scale(settings_t *settings,
return true;
}

bool command_event_save_auto_state(void)
size_t command_event_save_auto_state(void)
{
size_t _len;
runloop_state_t *runloop_st = runloop_state_get_ptr();
char savestate_name_auto[PATH_MAX_LENGTH];

if (runloop_st->entry_state_slot)
return false;
return 0;
if (!core_info_current_supports_savestate())
return false;
return 0;
if (string_is_empty(path_basename(path_get(RARCH_PATH_BASENAME))))
return false;

return 0;
_len = strlcpy(savestate_name_auto,
runloop_st->name.savestate,
sizeof(savestate_name_auto));
strlcpy(savestate_name_auto + _len, ".auto",
sizeof(savestate_name_auto) - _len);

_len += strlcpy(savestate_name_auto + _len, ".auto",
sizeof(savestate_name_auto) - _len);
if (content_auto_save_state((const char*)savestate_name_auto))
RARCH_LOG("[State]: %s \"%s\" %s.\n",
msg_hash_to_str(MSG_AUTO_SAVE_STATE_TO),
Expand All @@ -1282,8 +1278,7 @@ bool command_event_save_auto_state(void)
RARCH_LOG("[State]: %s \"%s\" %s.\n",
msg_hash_to_str(MSG_AUTO_SAVE_STATE_TO),
savestate_name_auto, "failed");

return true;
return _len;
}

#ifdef HAVE_CHEATS
Expand Down Expand Up @@ -1740,25 +1735,23 @@ void command_event_set_replay_garbage_collect(
{
/* TODO: debugme */
size_t i, cnt = 0;
char state_base[128];
char state_dir[DIR_MAX_LENGTH];
char tmp[DIR_MAX_LENGTH];
runloop_state_t *runloop_st = runloop_state_get_ptr();

struct string_list *dir_list = NULL;
unsigned min_idx = UINT_MAX;
const char *oldest_save = NULL;

/* Similar to command_event_set_replay_auto_index(),
* this will find the lowest numbered replay */
fill_pathname_basedir(state_dir, runloop_st->name.replay,
sizeof(state_dir));
fill_pathname_basedir(tmp, runloop_st->name.replay,
sizeof(tmp));

if (!(dir_list = dir_list_new_special(state_dir,
if (!(dir_list = dir_list_new_special(tmp,
DIR_LIST_PLAIN, NULL, show_hidden_files)))
return;

fill_pathname_base(state_base, runloop_st->name.replay,
sizeof(state_base));
fill_pathname_base(tmp, runloop_st->name.replay,
sizeof(tmp));

for (i = 0; i < dir_list->size; i++)
{
Expand All @@ -1783,7 +1776,7 @@ void command_event_set_replay_garbage_collect(

/* Check whether this file is associated with
* the current content */
if (!string_starts_with(elem_base, state_base))
if (!string_starts_with(elem_base, tmp))
continue;

/* This looks like a valid save */
Expand Down Expand Up @@ -1966,17 +1959,11 @@ void command_event_save_current_config(enum override_type type)
else
{
if (runloop_st->flags & RUNLOOP_FLAG_OVERRIDES_ACTIVE)
{
_len = strlcpy(msg, msg_hash_to_str(MSG_OVERRIDES_ACTIVE_NOT_SAVING), sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
else
{
_len = command_event_save_config(path_get(RARCH_PATH_CONFIG), msg, sizeof(msg));
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
runloop_msg_queue_push(msg, _len, 1, 180, true, NULL,
MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
}
}
break;
Expand Down Expand Up @@ -2061,7 +2048,7 @@ void command_event_remove_current_config(enum override_type type)
bool command_event_main_state(unsigned cmd)
{
char msg[128];
char state_path[16384];
char state_path[16384]; /* TODO/FIXME - reduce this */
size_t _len = 0;
settings_t *settings = config_get_ptr();
bool savestates_enabled = core_info_current_supports_savestate();
Expand All @@ -2081,8 +2068,13 @@ bool command_event_main_state(unsigned cmd)

/* TODO: Load state should act in one of three ways:
- [X] Not during recording or playback: normally
- [-] During playback: If the state is part of this replay, go back to that state and rewind the replay (not yet implemented); otherwise halt playback and go to that state normally.
- [-] During recording: If the state is part of this replay, go back to that state and rewind the replay, clobbering the stuff in between then and now (not yet implemented); if the state is not part of the replay, do nothing and log a warning.
- [-] During playback: If the state is part of this replay, go back to
that state and rewind the replay (not yet implemented); otherwise
halt playback and go to that state normally.
- [-] During recording: If the state is part of this replay, go back to
that state and rewind the replay, clobbering the stuff in between
then and now (not yet implemented); if the state is not part of
the replay, do nothing and log a warning.
*/


Expand All @@ -2093,7 +2085,8 @@ bool command_event_main_state(unsigned cmd)
case CMD_EVENT_SAVE_STATE:
case CMD_EVENT_SAVE_STATE_TO_RAM:
{
/* TODO: Saving state during recording should associate the state with the replay. */
/* TODO: Saving state during recording should associate
* the state with the replay. */
video_driver_state_t *video_st =
video_state_get_ptr();
bool savestate_auto_index =
Expand Down Expand Up @@ -2136,7 +2129,11 @@ bool command_event_main_state(unsigned cmd)
break;
case CMD_EVENT_UNDO_LOAD_STATE:
{
/* TODO: To support this through re-recording would take some care around moving the replay recording forward to the time when the undo happened, which would need undo support for replays. For now, forbid it during recording and halt playback. */
/* TODO: To support this through re-recording would take some
* care around moving the replay recording forward to the time
* when the undo happened, which would need undo support for
* replays. For now, forbid it during recording and halt
* playback. */
#ifdef HAVE_BSV_MOVIE
input_driver_state_t *input_st = input_state_get_ptr();
if (input_st->bsv_movie_state.flags & BSV_FLAG_MOVIE_RECORDING)
Expand Down Expand Up @@ -2179,8 +2176,8 @@ bool command_event_disk_control_append_image(
{
runloop_state_t *runloop_st = runloop_state_get_ptr();
rarch_system_info_t *sys_info = runloop_st ? (rarch_system_info_t*)&runloop_st->system : NULL;
if ( !sys_info ||
!disk_control_append_image(&sys_info->disk_control, path))
if ( !sys_info
|| !disk_control_append_image(&sys_info->disk_control, path))
return false;

#ifdef HAVE_THREADS
Expand Down Expand Up @@ -2230,12 +2227,11 @@ void command_event_reinit(const int flags)

video_driver_reinit(flags);
/* Poll input to avoid possibly stale data to corrupt things. */
if ( joypad && joypad->poll)
if (joypad && joypad->poll)
joypad->poll();
if ( sec_joypad && sec_joypad->poll)
if (sec_joypad && sec_joypad->poll)
sec_joypad->poll();
if ( input_st->current_driver &&
input_st->current_driver->poll)
if (input_st->current_driver && input_st->current_driver->poll)
input_st->current_driver->poll(input_st->current_data);
command_event(CMD_EVENT_GAME_FOCUS_TOGGLE, &game_focus_cmd);

Expand Down
2 changes: 1 addition & 1 deletion command.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ void command_event_set_mixer_volume(
bool command_event_resize_windowed_scale(settings_t *settings,
unsigned window_scale);

bool command_event_save_auto_state(void);
size_t command_event_save_auto_state(void);

/**
* event_set_volume:
Expand Down
25 changes: 8 additions & 17 deletions core_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -849,8 +849,7 @@ static bool core_info_cache_write(core_info_cache_list_t *list, const char *info
sizeof(file_path));

#if defined(CORE_INFO_CACHE_COMPRESS)
file = intfstream_open_rzip_file(file_path,
RETRO_VFS_FILE_ACCESS_WRITE);
file = intfstream_open_rzip_file(file_path, RETRO_VFS_FILE_ACCESS_WRITE);
#else
file = intfstream_open_file(file_path,
RETRO_VFS_FILE_ACCESS_WRITE,
Expand Down Expand Up @@ -1255,10 +1254,8 @@ bool core_info_cache_force_refresh(const char *path_info)
* if required */
if (!path_is_valid(file_path))
{
RFILE *refresh_file = filestream_open(
file_path,
RETRO_VFS_FILE_ACCESS_WRITE,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
RFILE *refresh_file = filestream_open(file_path,
RETRO_VFS_FILE_ACCESS_WRITE, RETRO_VFS_FILE_ACCESS_HINT_NONE);

if (!refresh_file)
return false;
Expand Down Expand Up @@ -1402,8 +1399,7 @@ static core_path_list_t *core_info_path_list_new(const char *core_dir,

/* Fetch core directory listing */
dir_list_ok = dir_list_append(path_list->dir_list,
core_dir, exts, false, show_hidden_files,
false, false);
core_dir, exts, false, show_hidden_files, false, false);

#if defined(__WINRT__) || defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
{
Expand Down Expand Up @@ -2196,19 +2192,15 @@ static bool core_info_does_support_file(
const core_info_t *core, const char *path)
{
const char *basename, *ext;

if (!core || !core->supported_extensions_list)
return false;
if (string_is_empty(path))
return false;

basename = path_basename(path);

/* if a core has / in its list of supported extensions, the core
supports loading of directories on the host file system */
if (string_is_empty(basename))
return string_list_find_elem(core->supported_extensions_list, "/");

ext = strrchr(basename, '.');
return string_list_find_elem_prefix(
core->supported_extensions_list, ".", (ext ? ext + 1 : ""));
Expand All @@ -2226,12 +2218,11 @@ static int core_info_qsort_cmp(const void *a_, const void *b_)
int support_b = core_info_does_support_file(b,
p_coreinfo->tmp_path);
#ifdef HAVE_COMPRESSION
support_a = support_a ||
core_info_does_support_any_file(a, p_coreinfo->tmp_list);
support_b = support_b ||
core_info_does_support_any_file(b, p_coreinfo->tmp_list);
support_a = support_a
|| core_info_does_support_any_file(a, p_coreinfo->tmp_list);
support_b = support_b
|| core_info_does_support_any_file(b, p_coreinfo->tmp_list);
#endif

if (support_a != support_b)
return support_b - support_a;
return strcasecmp(a->display_name, b->display_name);
Expand Down
8 changes: 4 additions & 4 deletions disk_control_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ unsigned disk_control_get_image_index(
**/
void disk_control_get_image_label(
disk_control_interface_t *disk_control,
unsigned index, char *label, size_t len)
unsigned index, char *s, size_t len)
{
if (!label || len < 1)
if (!s || len < 1)
return;

if (!disk_control)
Expand All @@ -254,13 +254,13 @@ void disk_control_get_image_label(
if (!disk_control->cb.get_image_label)
goto error;

if (!disk_control->cb.get_image_label(index, label, len))
if (!disk_control->cb.get_image_label(index, s, len))
goto error;

return;

error:
label[0] = '\0';
s[0] = '\0';
}

/***********/
Expand Down
4 changes: 2 additions & 2 deletions disk_control_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ bool disk_control_append_enabled(
*
* Leaf function.
*
* @return true if core supports image labels
* @return true if core supports image labels
* - get_image_label
**/
bool disk_control_image_label_enabled(
Expand Down Expand Up @@ -155,7 +155,7 @@ unsigned disk_control_get_image_index(
**/
void disk_control_get_image_label(
disk_control_interface_t *disk_control,
unsigned index, char *label, size_t len);
unsigned index, char *s, size_t len);

/***********/
/* Setters */
Expand Down
Loading

0 comments on commit 0b32901

Please sign in to comment.