Skip to content

Commit

Permalink
less string copies
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Dec 24, 2024
1 parent 02bcbff commit de8f979
Show file tree
Hide file tree
Showing 29 changed files with 213 additions and 313 deletions.
1 change: 0 additions & 1 deletion command.c
Original file line number Diff line number Diff line change
Expand Up @@ -1434,7 +1434,6 @@ void command_event_load_auto_state(void)
static void scan_states(settings_t *settings,
unsigned *last_index, char *file_to_delete)
{

runloop_state_t *runloop_st = runloop_state_get_ptr();
bool show_hidden_files = settings->bools.show_hidden_files;
unsigned savestate_max_keep = settings->uints.savestate_max_keep;
Expand Down
7 changes: 3 additions & 4 deletions frontend/drivers/platform_ctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,11 @@ static enum frontend_powerstate frontend_ctr_get_powerstate(
return FRONTEND_POWERSTATE_ON_POWER_SOURCE;
}

static void frontend_ctr_get_os(char* s, size_t len, int* major, int* minor)
static size_t frontend_ctr_get_os(char* s, size_t len, int* major, int* minor)
{
OS_VersionBin cver;
OS_VersionBin nver;

strlcpy(s, "3DS OS", len);
size_t _len = strlcpy(s, "3DS OS", len);
Result data_invalid = osGetSystemVersionData(&nver, &cver);
if (data_invalid == 0)
{
Expand All @@ -585,7 +584,7 @@ static void frontend_ctr_get_os(char* s, size_t len, int* major, int* minor)
*major = 0;
*minor = 0;
}

return _len;
}

static void frontend_ctr_get_name(char* s, size_t len)
Expand Down
37 changes: 14 additions & 23 deletions frontend/drivers/platform_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -280,31 +280,24 @@ static void frontend_darwin_get_name(char *s, size_t len)
#endif
}

static void frontend_darwin_get_os(char *s, size_t len, int *major, int *minor)
static size_t frontend_darwin_get_os(char *s, size_t len, int *major, int *minor)
{
size_t _len;
#if defined(IOS)
get_ios_version(major, minor);
#if TARGET_OS_TV
s[0] = 't';
s[1] = 'v';
s[2] = 'O';
s[3] = 'S';
s[4] = '\0';
_len = strlcpy(s, "tvOS", len);
#else
s[0] = 'i';
s[1] = 'O';
s[2] = 'S';
s[3] = '\0';
_len = strlcpy(s, "iOS", len);
#endif
#elif defined(OSX)

#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101300 /* MAC_OS_X_VERSION_10_13 */
NSOperatingSystemVersion version = NSProcessInfo.processInfo.operatingSystemVersion;
*major = (int)version.majorVersion;
*minor = (int)version.minorVersion;
#else
/* MacOS 10.9 includes the [NSProcessInfo operatingSystemVersion] function, but it's not in the 10.9 SDK. So, call it via NSInvocation */
/* Credit: OpenJDK (https://github.com/openjdk/jdk/commit/d4c7db50) */
/* MacOS 10.9 includes the [NSProcessInfo operatingSystemVersion] function, but it's not in the 10.9 SDK. So, call it via NSInvocation */
/* Credit: OpenJDK (https://github.com/openjdk/jdk/commit/d4c7db50) */
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(operatingSystemVersion)])
{
typedef struct
Expand All @@ -313,12 +306,12 @@ static void frontend_darwin_get_os(char *s, size_t len, int *major, int *minor)
NSInteger minorVersion;
NSInteger patchVersion;
} NSMyOSVersion;
NSMyOSVersion version;
NSMethodSignature *sig = [[NSProcessInfo processInfo] methodSignatureForSelector:@selector(operatingSystemVersion)];
NSInvocation *invoke = [NSInvocation invocationWithMethodSignature:sig];
invoke.selector = @selector(operatingSystemVersion);
[invoke invokeWithTarget:[NSProcessInfo processInfo]];
[invoke getReturnValue:&version];
NSMyOSVersion version;
NSMethodSignature *sig = [[NSProcessInfo processInfo] methodSignatureForSelector:@selector(operatingSystemVersion)];
NSInvocation *invoke = [NSInvocation invocationWithMethodSignature:sig];
invoke.selector = @selector(operatingSystemVersion);
[invoke invokeWithTarget:[NSProcessInfo processInfo]];
[invoke getReturnValue:&version];
*major = (int)version.majorVersion;
*minor = (int)version.minorVersion;
}
Expand All @@ -328,11 +321,9 @@ static void frontend_darwin_get_os(char *s, size_t len, int *major, int *minor)
Gestalt(gestaltSystemVersionMajor, (SInt32*)major);
}
#endif
s[0] = 'O';
s[1] = 'S';
s[2] = 'X';
s[3] = '\0';
_len = strlcpy(s, "OSX", len);
#endif
return _len;
}

static void frontend_darwin_get_env(int *argc, char *argv[],
Expand Down
17 changes: 9 additions & 8 deletions frontend/drivers/platform_switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ extern bool nxlink_connected;

void libnx_apply_overclock(void)
{
const size_t profiles_count = sizeof(SWITCH_CPU_PROFILES)
const size_t profiles_count = sizeof(SWITCH_CPU_PROFILES)
/ sizeof(SWITCH_CPU_PROFILES[1]);
settings_t *settings = config_get_ptr();
unsigned libnx_overclock = settings->uints.libnx_overclock;
Expand Down Expand Up @@ -565,7 +565,7 @@ static void frontend_switch_init(void *data)
bool recording_supported = false;

nifmInitialize(NifmServiceType_User);

if (hosversionBefore(8, 0, 0))
pcvInitialize();
else
Expand Down Expand Up @@ -609,8 +609,8 @@ static int frontend_switch_parse_drive_list(void *data, bool load_content)
{
#ifndef IS_SALAMANDER
file_list_t *list = (file_list_t *)data;
enum msg_hash_enums enum_idx = load_content
? MENU_ENUM_LABEL_FILE_DETECT_CORE_LIST_PUSH_DIR
enum msg_hash_enums enum_idx = load_content
? MENU_ENUM_LABEL_FILE_DETECT_CORE_LIST_PUSH_DIR
: MENU_ENUM_LABEL_FILE_BROWSER_DIRECTORY;

if (!list)
Expand All @@ -637,7 +637,7 @@ static uint64_t frontend_switch_get_total_mem(void)
return mem_info.usmblks;
}

static enum frontend_powerstate
static enum frontend_powerstate
frontend_switch_get_powerstate(int *seconds, int *percent)
{
uint32_t pct;
Expand Down Expand Up @@ -671,9 +671,10 @@ frontend_switch_get_powerstate(int *seconds, int *percent)
return FRONTEND_POWERSTATE_NO_SOURCE;
}

static void frontend_switch_get_os(
static size_t frontend_switch_get_os(
char *s, size_t len, int *major, int *minor)
{
size_t _len;
#ifdef HAVE_LIBNX
u32 hosVersion;
#else
Expand All @@ -684,7 +685,7 @@ static void frontend_switch_get_os(
ipc_request_t rq;
#endif

strlcpy(s, "Horizon OS", len);
_len = strlcpy(s, "Horizon OS", len);

#ifdef HAVE_LIBNX
*major = 0;
Expand Down Expand Up @@ -716,8 +717,8 @@ static void frontend_switch_get_os(
fail_sm:
sm_finalize();
fail:
return;
#endif
return _len;
}

static void frontend_switch_get_name(char *s, size_t len)
Expand Down
23 changes: 11 additions & 12 deletions frontend/drivers/platform_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1240,39 +1240,38 @@ static enum frontend_architecture frontend_unix_get_arch(void)
return FRONTEND_ARCH_NONE;
}

static void frontend_unix_get_os(char *s,
static size_t frontend_unix_get_os(char *s,
size_t len, int *major, int *minor)
{
size_t _len = 0;
#ifdef ANDROID
int rel;
frontend_android_get_version(major, minor, &rel);

strlcpy(s, "Android", len);
_len = strlcpy(s, "Android", len);
#else
char *ptr;
struct utsname buffer;

if (uname(&buffer) != 0)
return;

This comment has been minimized.

Copy link
@viachaslavic

viachaslavic Dec 25, 2024

Contributor

@LibretroAdmin must return size_t (_len) for now


*major = (int)strtol(buffer.release, &ptr, 10);
*minor = (int)strtol(++ptr, NULL, 10);
#if defined(__FreeBSD__)
strlcpy(s, "FreeBSD", len);
_len = strlcpy(s, "FreeBSD", len);
#elif defined(__NetBSD__)
strlcpy(s, "NetBSD", len);
_len = strlcpy(s, "NetBSD", len);
#elif defined(__OpenBSD__)
strlcpy(s, "OpenBSD", len);
_len = strlcpy(s, "OpenBSD", len);
#elif defined(__DragonFly__)
strlcpy(s, "DragonFly BSD", len);
_len = strlcpy(s, "DragonFly BSD", len);
#elif defined(BSD)
strlcpy(s, "BSD", len);
_len = strlcpy(s, "BSD", len);
#elif defined(__HAIKU__)
strlcpy(s, "Haiku", len);
_len = strlcpy(s, "Haiku", len);
#else
strlcpy(s, "Linux", len);
_len = strlcpy(s, "Linux", len);
#endif
#endif
return _len;
}

#ifdef HAVE_LAKKA
Expand Down
3 changes: 2 additions & 1 deletion frontend/drivers/platform_uwp.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

#include "../../uwp/uwp_func.h"

static void frontend_uwp_get_os(char *s, size_t len, int *major, int *minor)
static size_t frontend_uwp_get_os(char *s, size_t len, int *major, int *minor)
{
size_t _len;
char build_str[11] = {0};
Expand Down Expand Up @@ -134,6 +134,7 @@ static void frontend_uwp_get_os(char *s, size_t len, int *major, int *minor)
_len += strlcpy(s + _len, " ", len - _len);
strlcpy(s + _len, uwp_device_family, len - _len);
}
return _len;
}

static void frontend_uwp_init(void *data) { }
Expand Down
3 changes: 2 additions & 1 deletion frontend/drivers/platform_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ static void gfx_set_dwm(void)
g_plat_win32_flags |= PLAT_WIN32_FLAG_DWM_COMPOSITION_DISABLED;
}

static void frontend_win32_get_os(char *s, size_t len, int *major, int *minor)
static size_t frontend_win32_get_os(char *s, size_t len, int *major, int *minor)
{
size_t _len = 0;
char build_str[11] = {0};
Expand Down Expand Up @@ -424,6 +424,7 @@ static void frontend_win32_get_os(char *s, size_t len, int *major, int *minor)
_len += strlcpy(s + _len, " ", len - _len);
strlcpy(s + _len, vi.szCSDVersion, len - _len);
}
return _len;
}

static void frontend_win32_init(void *data)
Expand Down
2 changes: 1 addition & 1 deletion frontend/frontend_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ typedef struct frontend_ctx_driver
bool (*set_fork)(enum frontend_fork fork_mode);
void (*shutdown)(bool);
void (*get_name)(char *, size_t);
void (*get_os)(char *, size_t, int *major, int *minor);
size_t (*get_os)(char *, size_t, int *major, int *minor);
int (*get_rating)(void);
void (*content_loaded)(void);
enum frontend_architecture (*get_architecture)(void);
Expand Down
2 changes: 1 addition & 1 deletion gfx/common/win32_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ static bool win32_browser(
* FIXME: We should really handle the
* error case when this isn't big enough. */
char new_title[PATH_MAX];
char new_file[32768];
char new_file[32768]; /* TODO/FIXME - is this not way too big? */

new_title[0] = '\0';
new_file[0] = '\0';
Expand Down
2 changes: 1 addition & 1 deletion gfx/drivers_shader/shader_gl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,7 @@ void Pass::reflect_parameter_array(const char *name, std::vector<slang_texture_s
frag = glGetUniformLocation(pipeline, frag_n);

if (vert >= 0)
m->location.push_vertex = vert;
m->location.push_vertex = vert;
if (frag >= 0)
m->location.push_fragment = frag;
}
Expand Down
4 changes: 1 addition & 3 deletions gfx/gfx_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ typedef struct gfx_display_ctx_coord_draw

typedef struct gfx_display_ctx_datetime
{
char *s;
size_t len;
unsigned time_mode;
unsigned date_separator;
} gfx_display_ctx_datetime_t;
Expand Down Expand Up @@ -297,7 +295,7 @@ bool gfx_display_reset_icon_texture(
const char *texture_path,
uintptr_t *item, enum texture_filter_type filter_type,
unsigned *width, unsigned *height);

bool gfx_display_reset_textures_list_buffer(
uintptr_t *item,
enum texture_filter_type filter_type,
Expand Down
6 changes: 3 additions & 3 deletions gfx/gfx_thumbnail_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ bool gfx_thumbnail_set_content_image(
img_dir, img_name, sizeof(path_data->content_path));

/* Set core name to "imageviewer" */
strlcpy(
path_data->content_core_name,
"imageviewer", sizeof(path_data->content_core_name));
strlcpy(path_data->content_core_name,
"imageviewer",
sizeof(path_data->content_core_name));

/* Set database name (arbitrarily) to "_images_"
* (required for compatibility with gfx_thumbnail_update_path(),
Expand Down
14 changes: 8 additions & 6 deletions gfx/widgets/gfx_widget_load_content_animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ bool gfx_widget_start_load_content_animation(void)

/* We can only use the core db_name if the
* core is associated with exactly one database */
if (databases_list &&
(databases_list->size == 1))
if ( databases_list
&& (databases_list->size == 1))
core_db_name = databases_list->elems[0].data;

if ( !string_is_empty(core_db_name)
Expand Down Expand Up @@ -580,8 +580,9 @@ static void gfx_widget_load_content_animation_layout(
state->system_name_width = (system_name_width > 0) ?
(unsigned)system_name_width : 0;

text_width = (state->content_name_width > state->system_name_width) ?
(int)state->content_name_width : (int)state->system_name_width;
text_width = (state->content_name_width > state->system_name_width)
? (int)state->content_name_width
: (int)state->system_name_width;

/* Now we have the text width, can determine
* final icon/text x draw positions */
Expand Down Expand Up @@ -637,8 +638,9 @@ static void gfx_widget_load_content_animation_iterate(void *user_data,
state->system_name_width = (system_name_width > 0) ?
(unsigned)system_name_width : 0;

text_width = (state->content_name_width > state->system_name_width) ?
(int)state->content_name_width : (int)state->system_name_width;
text_width = (state->content_name_width > state->system_name_width)
? (int)state->content_name_width
: (int)state->system_name_width;

/* Now we have the text width, can determine
* final icon/text x draw positions */
Expand Down
11 changes: 4 additions & 7 deletions menu/cbs/menu_cbs_deferred_push.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,13 @@ static int general_push(menu_displaylist_info_t *info,
else
{
char tmp_path[PATH_MAX_LENGTH];
fill_pathname_expand_special(tmp_path, menu->scratch2_buf, sizeof(tmp_path));
const char *menu_path = tmp_path;
fill_pathname_join_special(tmp_str, menu_path,
fill_pathname_expand_special(tmp_path,
menu->scratch2_buf, sizeof(tmp_path));
fill_pathname_join_special(tmp_str, tmp_path,
menu->scratch_buf, sizeof(tmp_str));
}
#else
const char *menu_path = menu->scratch2_buf;
fill_pathname_join_special(tmp_str, menu_path,
fill_pathname_join_special(tmp_str, menu->scratch2_buf,
menu->scratch_buf, sizeof(tmp_str));
#endif

Expand Down Expand Up @@ -517,11 +516,9 @@ static int general_push(menu_displaylist_info_t *info,
bool filter_by_current_core = settings->bools.filter_by_current_core;

if (sysinfo && !string_is_empty(sysinfo->valid_extensions))
{
_len += strlcpy(newstr2 + _len,
sysinfo->valid_extensions,
sizeof(newstr2) - _len);
}

if (!filter_by_current_core)
{
Expand Down
Loading

0 comments on commit de8f979

Please sign in to comment.