From 0e85113eb3583a30cb36e21343c79d57029aa933 Mon Sep 17 00:00:00 2001 From: libretroadmin Date: Sun, 10 Jul 2022 18:13:25 +0200 Subject: [PATCH] (libretro-common) Style nits/cleanups --- gfx/display_servers/dispserv_win32.c | 6 +- gfx/gfx_thumbnail.c | 46 +++++----- libretro-common/encodings/encoding_utf.c | 106 +++++++++-------------- libretro-common/file/config_file.c | 86 ++++++++---------- libretro-common/file/file_path.c | 37 +++----- libretro-common/file/file_path_io.c | 6 +- libretro-common/lists/file_list.c | 7 +- libretro-common/lists/string_list.c | 79 +++++++---------- libretro-common/queues/task_queue.c | 13 ++- libretro-common/rthreads/rthreads.c | 97 ++++++++------------- libretro-common/string/stdstring.c | 55 +++++------- 11 files changed, 211 insertions(+), 327 deletions(-) diff --git a/gfx/display_servers/dispserv_win32.c b/gfx/display_servers/dispserv_win32.c index e65c2887df..f2d43316b7 100644 --- a/gfx/display_servers/dispserv_win32.c +++ b/gfx/display_servers/dispserv_win32.c @@ -373,10 +373,8 @@ static void *win32_display_server_get_resolution_list( } *len = count; - conf = (struct video_display_config*) - calloc(*len, sizeof(struct video_display_config)); - - if (!conf) + if (!(conf = (struct video_display_config*) + calloc(*len, sizeof(struct video_display_config)))) return NULL; for (i = 0, j = 0; win32_get_video_output(&dm, i, sizeof(dm)); i++) diff --git a/gfx/gfx_thumbnail.c b/gfx/gfx_thumbnail.c index 38ad59b674..d739e7fad9 100644 --- a/gfx/gfx_thumbnail.c +++ b/gfx/gfx_thumbnail.c @@ -364,16 +364,12 @@ void gfx_thumbnail_request_file( thumbnail->status = GFX_THUMBNAIL_STATUS_MISSING; /* Check if file path is valid */ - if (string_is_empty(file_path)) - return; - - if (!path_is_valid(file_path)) + if ( string_is_empty(file_path) + || !path_is_valid(file_path)) return; /* Load thumbnail */ - thumbnail_tag = (gfx_thumbnail_tag_t*)malloc(sizeof(gfx_thumbnail_tag_t)); - - if (!thumbnail_tag) + if (!(thumbnail_tag = (gfx_thumbnail_tag_t*)malloc(sizeof(gfx_thumbnail_tag_t)))) return; /* Configure user data */ @@ -788,17 +784,22 @@ void gfx_thumbnail_get_draw_dimensions( unsigned width, unsigned height, float scale_factor, float *draw_width, float *draw_height) { - video_driver_state_t *video_st = video_state_get_ptr(); + float core_aspect; float display_aspect; float thumbnail_aspect; - float core_aspect; + video_driver_state_t *video_st = video_state_get_ptr(); /* Sanity check */ - if (!thumbnail || (width < 1) || (height < 1)) - goto error; - - if ((thumbnail->width < 1) || (thumbnail->height < 1)) - goto error; + if ( !thumbnail + || (width < 1) + || (height < 1) + || (thumbnail->width < 1) + || (thumbnail->height < 1)) + { + *draw_width = 0.0f; + *draw_height = 0.0f; + return; + } /* Account for display/thumbnail/core aspect ratio * differences */ @@ -842,11 +843,6 @@ void gfx_thumbnail_get_draw_dimensions( * without scaling manually... */ *draw_width *= scale_factor; *draw_height *= scale_factor; - return; - -error: - *draw_width = 0.0f; - *draw_height = 0.0f; } /* Draws specified thumbnail with specified alignment @@ -872,10 +868,14 @@ void gfx_thumbnail_draw( gfx_display_t *p_disp = disp_get_ptr(); gfx_display_ctx_driver_t *dispctx = p_disp->dispctx; /* Sanity check */ - if (!thumbnail || - (width < 1) || (height < 1) || (alpha <= 0.0f) || (scale_factor <= 0.0f)) - return; - if (!dispctx) + if ( + !thumbnail + || !dispctx + || (width < 1) + || (height < 1) + || (alpha <= 0.0f) + || (scale_factor <= 0.0f) + ) return; /* Only draw thumbnail if it is available... */ diff --git a/libretro-common/encodings/encoding_utf.c b/libretro-common/encodings/encoding_utf.c index 2760824d25..4b9eefea19 100644 --- a/libretro-common/encodings/encoding_utf.c +++ b/libretro-common/encodings/encoding_utf.c @@ -255,10 +255,9 @@ static bool utf16_to_char(uint8_t **utf_data, utf16_conv_utf8(NULL, dest_len, in, len); *dest_len += 1; *utf_data = (uint8_t*)malloc(*dest_len); - if (*utf_data == 0) - return false; - - return utf16_conv_utf8(*utf_data, dest_len, in, len); + if (*utf_data != 0) + return utf16_conv_utf8(*utf_data, dest_len, in, len); + return false; } bool utf16_to_char_string(const uint16_t *in, char *s, size_t len) @@ -300,10 +299,8 @@ static char *mb_to_mb_string_alloc(const char *str, if (!path_buf_wide_len) return strdup(str); - path_buf_wide = (wchar_t*) - calloc(path_buf_wide_len + sizeof(wchar_t), sizeof(wchar_t)); - - if (path_buf_wide) + if ((path_buf_wide = (wchar_t*) + calloc(path_buf_wide_len + sizeof(wchar_t), sizeof(wchar_t)))) { MultiByteToWideChar(cp_in, 0, str, -1, path_buf_wide, path_buf_wide_len); @@ -350,31 +347,29 @@ static char *mb_to_mb_string_alloc(const char *str, /* Returned pointer MUST be freed by the caller if non-NULL. */ char* utf8_to_local_string_alloc(const char *str) { - if (str && *str) - { #if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE) + if (str && *str) return mb_to_mb_string_alloc(str, CODEPAGE_UTF8, CODEPAGE_LOCAL); #else - /* assume string needs no modification if not on Windows */ + /* Assume string needs no modification if not on Windows */ + if (str && *str) return strdup(str); #endif - } return NULL; } /* Returned pointer MUST be freed by the caller if non-NULL. */ char* local_to_utf8_string_alloc(const char *str) { - if (str && *str) - { #if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE) - return mb_to_mb_string_alloc(str, CODEPAGE_LOCAL, CODEPAGE_UTF8); + if (str && *str) + return mb_to_mb_string_alloc(str, CODEPAGE_LOCAL, CODEPAGE_UTF8); #else - /* assume string needs no modification if not on Windows */ + /* Assume string needs no modification if not on Windows */ + if (str && *str) return strdup(str); #endif - } - return NULL; + return NULL; } /* Returned pointer MUST be freed by the caller if non-NULL. */ @@ -382,10 +377,8 @@ wchar_t* utf8_to_utf16_string_alloc(const char *str) { #ifdef _WIN32 int len = 0; - int out_len = 0; #else size_t len = 0; - size_t out_len = 0; #endif wchar_t *buf = NULL; @@ -393,56 +386,44 @@ wchar_t* utf8_to_utf16_string_alloc(const char *str) return NULL; #ifdef _WIN32 - len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); - - if (len) + if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0))) { - buf = (wchar_t*)calloc(len, sizeof(wchar_t)); - - if (!buf) + if (!(buf = (wchar_t*)calloc(len, sizeof(wchar_t)))) return NULL; - out_len = MultiByteToWideChar(CP_UTF8, 0, str, -1, buf, len); + if ((MultiByteToWideChar(CP_UTF8, 0, str, -1, buf, len)) < 0) + { + free(buf); + return NULL; + } } else { - /* fallback to ANSI codepage instead */ - len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); - - if (len) + /* Fallback to ANSI codepage instead */ + if ((len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0))) { - buf = (wchar_t*)calloc(len, sizeof(wchar_t)); - - if (!buf) + if (!(buf = (wchar_t*)calloc(len, sizeof(wchar_t)))) return NULL; - out_len = MultiByteToWideChar(CP_ACP, 0, str, -1, buf, len); + if ((MultiByteToWideChar(CP_ACP, 0, str, -1, buf, len)) < 0) + { + free(buf); + return NULL; + } } } - - if (out_len < 0) - { - free(buf); - return NULL; - } #else /* NOTE: For now, assume non-Windows platforms' locale is already UTF-8. */ - len = mbstowcs(NULL, str, 0) + 1; - - if (len) + if ((len = mbstowcs(NULL, str, 0) + 1)) { - buf = (wchar_t*)calloc(len, sizeof(wchar_t)); - - if (!buf) + if (!(buf = (wchar_t*)calloc(len, sizeof(wchar_t)))) return NULL; - out_len = mbstowcs(buf, str, len); - } - - if (out_len == (size_t)-1) - { - free(buf); - return NULL; + if ((mbstowcs(buf, str, len)) == (size_t)-1) + { + free(buf); + return NULL; + } } #endif @@ -465,20 +446,17 @@ char* utf16_to_utf8_string_alloc(const wchar_t *str) #ifdef _WIN32 { UINT code_page = CP_UTF8; - len = WideCharToMultiByte(code_page, - 0, str, -1, NULL, 0, NULL, NULL); /* fallback to ANSI codepage instead */ - if (!len) + if (!(len = WideCharToMultiByte(code_page, + 0, str, -1, NULL, 0, NULL, NULL))) { code_page = CP_ACP; len = WideCharToMultiByte(code_page, 0, str, -1, NULL, 0, NULL, NULL); } - buf = (char*)calloc(len, sizeof(char)); - - if (!buf) + if (!(buf = (char*)calloc(len, sizeof(char)))) return NULL; if (WideCharToMultiByte(code_page, @@ -491,13 +469,9 @@ char* utf16_to_utf8_string_alloc(const wchar_t *str) #else /* NOTE: For now, assume non-Windows platforms' * locale is already UTF-8. */ - len = wcstombs(NULL, str, 0) + 1; - - if (len) + if ((len = wcstombs(NULL, str, 0) + 1)) { - buf = (char*)calloc(len, sizeof(char)); - - if (!buf) + if (!(buf = (char*)calloc(len, sizeof(char)))) return NULL; if (wcstombs(buf, str, len) == (size_t)-1) diff --git a/libretro-common/file/config_file.c b/libretro-common/file/config_file.c index b3ab8949cc..c064ea6cec 100644 --- a/libretro-common/file/config_file.c +++ b/libretro-common/file/config_file.c @@ -521,17 +521,16 @@ static bool config_file_parse_line(config_file_t *conf, if (comment) { config_file_t sub_conf; - bool include_found = false; - bool reference_found = false; char real_path[PATH_MAX_LENGTH]; char *path = NULL; char *include_line = NULL; char *reference_line = NULL; - - include_found = string_starts_with_size(comment, "include ", - STRLEN_CONST("include ")); - reference_found = string_starts_with_size(comment, "reference ", - STRLEN_CONST("reference ")); + bool include_found = string_starts_with_size(comment, + "include ", + STRLEN_CONST("include ")); + bool reference_found = string_starts_with_size(comment, + "reference ", + STRLEN_CONST("reference ")); /* All comments except those starting with the include or * reference directive are ignored */ @@ -547,9 +546,7 @@ static bool config_file_parse_line(config_file_t *conf, if (string_is_empty(include_line)) return false; - path = config_file_extract_value(include_line, false); - - if (!path) + if (!(path = config_file_extract_value(include_line, false))) return false; if ( string_is_empty(path) @@ -590,9 +587,7 @@ static bool config_file_parse_line(config_file_t *conf, if (string_is_empty(reference_line)) return false; - path = config_file_extract_value(reference_line, false); - - if (!path) + if (!(path = config_file_extract_value(reference_line, false))) return false; config_file_add_reference(conf, path); @@ -610,8 +605,7 @@ static bool config_file_parse_line(config_file_t *conf, line++; /* Allocate storage for key */ - key = (char*)malloc(cur_size + 1); - if (!key) + if (!(key = (char*)malloc(cur_size + 1))) return false; /* Copy line contents into key until we @@ -623,15 +617,13 @@ static bool config_file_parse_line(config_file_t *conf, if (idx == cur_size) { cur_size *= 2; - key_tmp = (char*)realloc(key, cur_size + 1); - - if (!key_tmp) + if (!(key_tmp = (char*)realloc(key, cur_size + 1))) { free(key); return false; } - key = key_tmp; + key = key_tmp; } key[idx++] = *line++; @@ -640,10 +632,9 @@ static bool config_file_parse_line(config_file_t *conf, /* Add key and value entries to list */ list->key = key; - list->value = config_file_extract_value(line, true); /* An entry without a value is invalid */ - if (!list->value) + if (!(list->value = config_file_extract_value(line, true))) { list->key = NULL; free(key); @@ -768,9 +759,8 @@ bool config_file_deinitialize(config_file_t *conf) void config_file_free(config_file_t *conf) { - if (!config_file_deinitialize(conf)) - return; - free(conf); + if (config_file_deinitialize(conf)) + free(conf); } bool config_append_file(config_file_t *conf, const char *path) @@ -812,27 +802,24 @@ config_file_t *config_file_new_from_string(char *from_string, const char *path) { struct config_file *conf = config_file_new_alloc(); - - if (!conf) - return NULL; - if (config_file_from_string_internal(conf, from_string, path) == -1) - { + if ( conf + && config_file_from_string_internal( + conf, from_string, path) != -1) + return conf; + if (conf) config_file_free(conf); - return NULL; - } - return conf; + return NULL; } config_file_t *config_file_new_from_path_to_string(const char *path) { - int64_t length = 0; - uint8_t *ret_buf = NULL; - config_file_t *conf = NULL; - if (path_is_valid(path)) { + uint8_t *ret_buf = NULL; + int64_t length = 0; if (filestream_read_file(path, (void**)&ret_buf, &length)) { + config_file_t *conf = NULL; /* Note: 'ret_buf' is not used outside this * function - we do not care that it will be * modified by config_file_new_from_string() */ @@ -841,10 +828,12 @@ config_file_t *config_file_new_from_path_to_string(const char *path) if ((void*)ret_buf) free((void*)ret_buf); + + return conf; } } - return conf; + return NULL; } config_file_t *config_file_new_with_callback( @@ -854,8 +843,7 @@ config_file_t *config_file_new_with_callback( struct config_file *conf = config_file_new_alloc(); if (!path || !*path) return conf; - ret = config_file_load_internal(conf, path, 0, cb); - if (ret == -1) + if ((ret = config_file_load_internal(conf, path, 0, cb)) == -1) { config_file_free(conf); return NULL; @@ -874,8 +862,7 @@ config_file_t *config_file_new(const char *path) struct config_file *conf = config_file_new_alloc(); if (!path || !*path) return conf; - ret = config_file_load_internal(conf, path, 0, NULL); - if (ret == -1) + if ((ret = config_file_load_internal(conf, path, 0, NULL)) == -1) { config_file_free(conf); return NULL; @@ -918,16 +905,14 @@ static struct config_entry_list *config_get_entry_internal( const config_file_t *conf, const char *key, struct config_entry_list **prev) { - struct config_entry_list *entry = NULL; - struct config_entry_list *previous = prev ? *prev : NULL; - - entry = RHMAP_GET_STR(conf->entries_map, key); + struct config_entry_list *entry = RHMAP_GET_STR(conf->entries_map, key); if (entry) return entry; if (prev) { + struct config_entry_list *previous = *prev; for (entry = conf->entries; entry; entry = entry->next) previous = entry; @@ -1090,9 +1075,9 @@ bool config_get_string(config_file_t *conf, const char *key, char **str) bool config_get_config_path(config_file_t *conf, char *s, size_t len) { - if (!conf) - return false; - return strlcpy(s, conf->path, len); + if (conf) + return strlcpy(s, conf->path, len); + return false; } bool config_get_array(config_file_t *conf, const char *key, @@ -1226,9 +1211,8 @@ void config_unset(config_file_t *conf, const char *key) return; last = conf->entries; - entry = config_get_entry_internal(conf, key, &last); - if (!entry) + if (!(entry = config_get_entry_internal(conf, key, &last))) return; (void)RHMAP_DEL_STR(conf->entries_map, entry->key); @@ -1332,7 +1316,7 @@ bool config_file_write(config_file_t *conf, const char *path, bool sort) if (!file) return false; - buf = calloc(1, 0x4000); + buf = calloc(1, 0x4000); setvbuf(file, (char*)buf, _IOFBF, 0x4000); config_file_dump(conf, file, sort); diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index 36c20f1b09..d8f52c63b9 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -254,10 +254,9 @@ bool path_is_compressed_file(const char* path) { const char *ext = path_get_extension(path); if (!string_is_empty(ext)) - if ( string_is_equal_noncase(ext, "zip") || - string_is_equal_noncase(ext, "apk") || - string_is_equal_noncase(ext, "7z")) - return true; + return ( string_is_equal_noncase(ext, "zip") + || string_is_equal_noncase(ext, "apk") + || string_is_equal_noncase(ext, "7z")); return false; } @@ -457,7 +456,6 @@ void fill_pathname_basedir_noext(char *out_dir, bool fill_pathname_parent_dir_name(char *out_dir, const char *in_dir, size_t size) { - bool success = false; char *temp = strdup(in_dir); char *last = find_last_slash(temp); @@ -473,26 +471,24 @@ bool fill_pathname_parent_dir_name(char *out_dir, *last = '\0'; /* Point in_dir to the address of the last slash. */ - in_dir = find_last_slash(temp); - /* If find_last_slash returns NULL, it means there was no slash in temp, so use temp as-is. */ - if (!in_dir) + if (!(in_dir = find_last_slash(temp))) in_dir = temp; - success = in_dir && in_dir[1]; - - if (success) + if (in_dir && in_dir[1]) { /* If path starts with an slash, eliminate it. */ if (path_is_absolute(in_dir)) strlcpy(out_dir, in_dir + 1, size); else strlcpy(out_dir, in_dir, size); + free(temp); + return true; } free(temp); - return success; + return false; } /** @@ -591,9 +587,7 @@ void path_basedir(char *path) if (strlen(path) < 2) return; - last = find_last_slash(path); - - if (last) + if ((last = find_last_slash(path))) last[1] = '\0'; else strlcpy(path, "." PATH_DEFAULT_SLASH(), 3); @@ -690,15 +684,13 @@ bool path_is_absolute(const char *path) #if defined(_WIN32) /* Many roads lead to Rome... * Note: Drive letter can only be 1 character long */ - if (string_starts_with_size(path, "\\\\", STRLEN_CONST("\\\\")) || - string_starts_with_size(path + 1, ":/", STRLEN_CONST(":/")) || - string_starts_with_size(path + 1, ":\\", STRLEN_CONST(":\\"))) - return true; + return ( string_starts_with_size(path, "\\\\", STRLEN_CONST("\\\\")) + || string_starts_with_size(path + 1, ":/", STRLEN_CONST(":/")) + || string_starts_with_size(path + 1, ":\\", STRLEN_CONST(":\\"))); #elif defined(__wiiu__) || defined(VITA) { const char *seperator = strchr(path, ':'); - if (seperator && (seperator[1] == '/')) - return true; + return (seperator && (seperator[1] == '/')); } #endif @@ -804,8 +796,7 @@ char *path_resolve_realpath(char *buf, size_t size, bool resolve_symlinks) * if there are no slashes, they point relative to where one would be */ do { - next = strchr(p, '/'); - if (!next) + if (!(next = strchr(p, '/'))) next = buf_end; if ((next - p == 2 && p[0] == '.' && p[1] == '.')) diff --git a/libretro-common/file/file_path_io.c b/libretro-common/file/file_path_io.c index 63587e3928..f7e77e1749 100644 --- a/libretro-common/file/file_path_io.c +++ b/libretro-common/file/file_path_io.c @@ -118,10 +118,8 @@ bool path_mkdir(const char *dir) /* Use heap. Real chance of stack * overflow if we recurse too hard. */ - basedir = strdup(dir); - - if (!basedir) - return false; + if (!(basedir = strdup(dir))) + return false; path_parent_dir(basedir); diff --git a/libretro-common/lists/file_list.c b/libretro-common/lists/file_list.c index 6a8a12da47..8680b73145 100644 --- a/libretro-common/lists/file_list.c +++ b/libretro-common/lists/file_list.c @@ -75,9 +75,7 @@ bool file_list_reserve(file_list_t *list, size_t nitems) if (nitems < list->capacity || nitems > (size_t)-1/item_size) return false; - new_data = (struct item_file*)realloc(list->list, nitems * item_size); - - if (!new_data) + if (!(new_data = (struct item_file*)realloc(list->list, nitems * item_size))) return false; memset(&new_data[list->capacity], 0, item_size * (nitems - list->capacity)); @@ -430,8 +428,7 @@ bool file_list_search(const file_list_t *list, const char *needle, size_t *idx) continue; } - str = (const char *)strcasestr(alt, needle); - if (str == alt) + if ((str = (const char *)strcasestr(alt, needle)) == alt) { /* Found match with first chars, best possible match. */ *idx = i; diff --git a/libretro-common/lists/string_list.c b/libretro-common/lists/string_list.c index 0df12d7a18..e9a0fb0173 100644 --- a/libretro-common/lists/string_list.c +++ b/libretro-common/lists/string_list.c @@ -182,8 +182,7 @@ bool string_list_append(struct string_list *list, const char *elem, (list->cap > 0) ? (list->cap * 2) : 32)) return false; - data_dup = strdup(elem); - if (!data_dup) + if (!(data_dup = strdup(elem))) return false; list->elems[list->size].data = data_dup; @@ -213,9 +212,7 @@ bool string_list_append_n(struct string_list *list, const char *elem, !string_list_capacity(list, list->cap * 2)) return false; - data_dup = (char*)malloc(length + 1); - - if (!data_dup) + if (!(data_dup = (char*)malloc(length + 1))) return false; strlcpy(data_dup, elem, length + 1); @@ -298,8 +295,7 @@ struct string_list *string_split(const char *str, const char *delim) if (!list) return NULL; - copy = strdup(str); - if (!copy) + if (!(copy = strdup(str))) goto error; tmp = strtok_r(copy, delim, &save); @@ -334,8 +330,7 @@ bool string_split_noalloc(struct string_list *list, if (!list) return false; - copy = strdup(str); - if (!copy) + if (!(copy = strdup(str))) return false; tmp = strtok_r(copy, delim, &save); @@ -377,15 +372,13 @@ struct string_list *string_separate(char *str, const char *delim) /* Sanity check */ if (!str || string_is_empty(delim)) - goto error; + return NULL; + if (!(list = string_list_new())) + return NULL; str_ptr = &str; - list = string_list_new(); + token = string_tokenize(str_ptr, delim); - if (!list) - goto error; - - token = string_tokenize(str_ptr, delim); while (token) { union string_list_elem_attr attr; @@ -393,22 +386,17 @@ struct string_list *string_separate(char *str, const char *delim) attr.i = 0; if (!string_list_append(list, token, attr)) - goto error; + { + free(token); + string_list_free(list); + return NULL; + } free(token); - token = NULL; - token = string_tokenize(str_ptr, delim); } return list; - -error: - if (token) - free(token); - if (list) - string_list_free(list); - return NULL; } bool string_separate_noalloc( @@ -455,15 +443,14 @@ bool string_separate_noalloc( */ int string_list_find_elem(const struct string_list *list, const char *elem) { - size_t i; - - if (!list) - return false; - - for (i = 0; i < list->size; i++) + if (list) { - if (string_is_equal_noncase(list->elems[i].data, elem)) - return (int)(i + 1); + size_t i; + for (i = 0; i < list->size; i++) + { + if (string_is_equal_noncase(list->elems[i].data, elem)) + return (int)(i + 1); + } } return false; @@ -496,16 +483,15 @@ bool string_list_find_elem_prefix(const struct string_list *list, for (i = 0; i < list->size; i++) { - if (string_is_equal_noncase(list->elems[i].data, elem) || - string_is_equal_noncase(list->elems[i].data, prefixed)) + if ( string_is_equal_noncase(list->elems[i].data, elem) + || string_is_equal_noncase(list->elems[i].data, prefixed)) return true; } return false; } -struct string_list *string_list_clone( - const struct string_list *src) +struct string_list *string_list_clone(const struct string_list *src) { unsigned i; struct string_list_elem @@ -519,14 +505,13 @@ struct string_list *string_list_clone( dest->elems = NULL; dest->size = src->size; - dest->cap = src->cap; - if (dest->cap < dest->size) + if (src->cap < dest->size) dest->cap = dest->size; + else + dest->cap = src->cap; - elems = (struct string_list_elem*) - calloc(dest->cap, sizeof(struct string_list_elem)); - - if (!elems) + if (!(elems = (struct string_list_elem*) + calloc(dest->cap, sizeof(struct string_list_elem)))) { free(dest); return NULL; @@ -536,11 +521,11 @@ struct string_list *string_list_clone( for (i = 0; i < src->size; i++) { - const char *_src = src->elems[i].data; - size_t len = _src ? strlen(_src) : 0; + const char *_src = src->elems[i].data; + size_t len = _src ? strlen(_src) : 0; - dest->elems[i].data = NULL; - dest->elems[i].attr = src->elems[i].attr; + dest->elems[i].data = NULL; + dest->elems[i].attr = src->elems[i].attr; if (len != 0) { diff --git a/libretro-common/queues/task_queue.c b/libretro-common/queues/task_queue.c index 7d6b3c70c6..3a9d6c4875 100644 --- a/libretro-common/queues/task_queue.c +++ b/libretro-common/queues/task_queue.c @@ -323,17 +323,15 @@ static struct retro_task_impl impl_regular = { static void task_queue_remove(task_queue_t *queue, retro_task_t *task) { retro_task_t *t = NULL; - retro_task_t *front = NULL; - - front = queue->front; + retro_task_t *front = queue->front; /* Remove first element if needed */ if (task == front) { - queue->front = task->next; + queue->front = task->next; if (queue->back == task) /* if only element, also update back */ - queue->back = NULL; - task->next = NULL; + queue->back = NULL; + task->next = NULL; return; } @@ -485,8 +483,7 @@ static void threaded_worker(void *userdata) slock_lock(running_lock); /* Get first task to run */ - task = tasks_running.front; - if (!task) + if (!(task = tasks_running.front)) { scond_wait(worker_cond, running_lock); slock_unlock(running_lock); diff --git a/libretro-common/rthreads/rthreads.c b/libretro-common/rthreads/rthreads.c index 30ec6ffb56..02aa5be65d 100644 --- a/libretro-common/rthreads/rthreads.c +++ b/libretro-common/rthreads/rthreads.c @@ -193,9 +193,11 @@ sthread_t *sthread_create_with_priority(void (*thread_func)(void*), void *userda if (!thread) return NULL; - data = (struct thread_data*)malloc(sizeof(*data)); - if (!data) - goto error; + if (!(data = (struct thread_data*)malloc(sizeof(*data)))) + { + free(thread); + return NULL; + } data->func = thread_func; data->userdata = userdata; @@ -242,10 +244,7 @@ sthread_t *sthread_create_with_priority(void (*thread_func)(void*), void *userda if (thread_created) return thread; - -error: - if (data) - free(data); + free(data); free(thread); return NULL; } @@ -307,14 +306,10 @@ void sthread_join(sthread_t *thread) */ bool sthread_isself(sthread_t *thread) { - /* This thread can't possibly be a null thread */ - if (!thread) - return false; - #ifdef USE_WIN32_THREADS - return GetCurrentThreadId() == thread->id; + return thread ? GetCurrentThreadId() == thread->id : false; #else - return pthread_equal(pthread_self(),thread->id); + return thread ? pthread_equal(pthread_self(), thread->id) : false; #endif } #endif @@ -329,27 +324,19 @@ bool sthread_isself(sthread_t *thread) **/ slock_t *slock_new(void) { - bool mutex_created = false; slock_t *lock = (slock_t*)calloc(1, sizeof(*lock)); if (!lock) return NULL; - - #ifdef USE_WIN32_THREADS InitializeCriticalSection(&lock->lock); - mutex_created = true; #else - mutex_created = (pthread_mutex_init(&lock->lock, NULL) == 0); + if (pthread_mutex_init(&lock->lock, NULL) != 0) + { + free(lock); + return NULL; + } #endif - - if (!mutex_created) - goto error; - return lock; - -error: - free(lock); - return NULL; } /** @@ -399,12 +386,10 @@ void slock_lock(slock_t *lock) **/ bool slock_try_lock(slock_t *lock) { - if (!lock) - return false; #ifdef USE_WIN32_THREADS - return TryEnterCriticalSection(&lock->lock); + return lock && TryEnterCriticalSection(&lock->lock); #else - return pthread_mutex_trylock(&lock->lock)==0; + return lock && (pthread_mutex_trylock(&lock->lock) == 0); #endif } @@ -467,11 +452,9 @@ scond_t *scond_new(void) * * Note: We might could simplify this using vista+ condition variables, * but we wanted an XP compatible solution. */ - cond->event = CreateEvent(NULL, FALSE, FALSE, NULL); - if (!cond->event) + if (!(cond->event = CreateEvent(NULL, FALSE, FALSE, NULL))) goto error; - cond->hot_potato = CreateEvent(NULL, FALSE, FALSE, NULL); - if (!cond->hot_potato) + if (!(cond->hot_potato = CreateEvent(NULL, FALSE, FALSE, NULL))) { CloseHandle(cond->event); goto error; @@ -525,7 +508,6 @@ static bool _scond_wait_win32(scond_t *cond, slock_t *lock, DWORD dwMilliseconds static bool beginPeriod = false; DWORD tsBegin; #endif - DWORD waitResult; DWORD dwFinalTimeout = dwMilliseconds; /* Careful! in case we begin in the head, we don't do the hot potato stuff, @@ -545,9 +527,7 @@ static bool _scond_wait_win32(scond_t *cond, slock_t *lock, DWORD dwMilliseconds } if (performanceCounterFrequency.QuadPart == 0) - { QueryPerformanceFrequency(&performanceCounterFrequency); - } #else if (!beginPeriod) { @@ -570,9 +550,9 @@ static bool _scond_wait_win32(scond_t *cond, slock_t *lock, DWORD dwMilliseconds /* walk to the end of the linked list */ while (*ptr) - ptr = &((*ptr)->next); + ptr = &((*ptr)->next); - *ptr = &myentry; + *ptr = &myentry; myentry.next = NULL; cond->waiters++; @@ -746,18 +726,17 @@ void scond_wait(scond_t *cond, slock_t *lock) int scond_broadcast(scond_t *cond) { #ifdef USE_WIN32_THREADS - /* remember: we currently have mutex */ - if (cond->waiters == 0) - return 0; - - /* awaken everything which is currently queued up */ - if (cond->wakens == 0) - SetEvent(cond->event); - cond->wakens = cond->waiters; - - /* Since there is now at least one pending waken, the potato must be in play */ - SetEvent(cond->hot_potato); + /* Remember, we currently have mutex */ + if (cond->waiters != 0) + { + /* Awaken everything which is currently queued up */ + if (cond->wakens == 0) + SetEvent(cond->event); + cond->wakens = cond->waiters; + /* Since there is now at least one pending waken, the potato must be in play */ + SetEvent(cond->hot_potato); + } return 0; #else return pthread_cond_broadcast(&cond->cond); @@ -830,11 +809,6 @@ bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us) * Someone asking for a 0 timeout clearly wants immediate timeout. * Someone asking for a 1 timeout clearly wants an actual timeout * of the minimum length */ - - /* Someone asking for 1000 or 1001 timeout shouldn't - * accidentally get 2ms. */ - DWORD dwMilliseconds = timeout_us/1000; - /* The implementation of a 0 timeout here with pthreads is sketchy. * It isn't clear what happens if pthread_cond_timedwait is called with NOW. * Moreover, it is possible that this thread gets pre-empted after the @@ -845,9 +819,10 @@ bool scond_wait_timeout(scond_t *cond, slock_t *lock, int64_t timeout_us) if (timeout_us == 0) return false; else if (timeout_us < 1000) - dwMilliseconds = 1; - - return _scond_wait_win32(cond,lock,dwMilliseconds); + return _scond_wait_win32(cond, lock, 1); + /* Someone asking for 1000 or 1001 timeout shouldn't + * accidentally get 2ms. */ + return _scond_wait_win32(cond, lock, timeout_us / 1000); #else int ret; int64_t seconds, remainder; @@ -944,9 +919,9 @@ bool sthread_tls_set(sthread_tls_t *tls, const void *data) uintptr_t sthread_get_thread_id(sthread_t *thread) { - if (!thread) - return 0; - return (uintptr_t)thread->id; + if (thread) + return (uintptr_t)thread->id; + return 0; } uintptr_t sthread_get_current_thread_id(void) diff --git a/libretro-common/string/stdstring.c b/libretro-common/string/stdstring.c index 2029f3f68f..be9eff7e66 100644 --- a/libretro-common/string/stdstring.c +++ b/libretro-common/string/stdstring.c @@ -114,9 +114,8 @@ char *string_replace_substring(const char *in, } outlen = strlen(in) - pattern_len*numhits + replacement_len*numhits; - out = (char *)malloc(outlen+1); - if (!out) + if (!(out = (char *)malloc(outlen+1))) return NULL; outat = out; @@ -305,9 +304,7 @@ void word_wrap_wideglyph(char *dst, size_t dst_size, const char *src, int line_w while (*src != '\0') { - unsigned char_len; - - char_len = (unsigned)(utf8skip(src, 1) - src); + unsigned char_len = (unsigned)(utf8skip(src, 1) - src); counter_normalized += 100; /* Prevent buffer overflow */ @@ -315,7 +312,7 @@ void word_wrap_wideglyph(char *dst, size_t dst_size, const char *src, int line_w break; if (*src == ' ') - lastspace = dst; /* Remember the location of the whitespace */ + lastspace = dst; /* Remember the location of the whitespace */ else if (*src == '\n') { /* If newlines embedded in the input, @@ -419,25 +416,20 @@ char* string_tokenize(char **str, const char *delim) if (!str || string_is_empty(delim)) return NULL; - str_ptr = *str; /* Note: we don't check string_is_empty() here, * empty strings are valid */ - if (!str_ptr) + if (!(str_ptr = *str)) return NULL; /* Search for delimiter */ - delim_ptr = strstr(str_ptr, delim); - - if (delim_ptr) + if ((delim_ptr = strstr(str_ptr, delim))) token_len = delim_ptr - str_ptr; else token_len = strlen(str_ptr); /* Allocate token string */ - token = (char *)malloc((token_len + 1) * sizeof(char)); - - if (!token) + if (!(token = (char *)malloc((token_len + 1) * sizeof(char)))) return NULL; /* Copy token */ @@ -475,13 +467,13 @@ void string_remove_all_chars(char *str, char c) * with character 'replace' */ void string_replace_all_chars(char *str, char find, char replace) { - char *str_ptr = str; + if (!string_is_empty(str)) + { + char *str_ptr = str; + while ((str_ptr = strchr(str_ptr, find))) + *str_ptr++ = replace; + } - if (string_is_empty(str)) - return; - - while ((str_ptr = strchr(str_ptr, find))) - *str_ptr++ = replace; } /* Converts string to unsigned integer. @@ -515,9 +507,7 @@ unsigned string_hex_to_unsigned(const char *str) return 0; /* Remove leading '0x', if required */ - len = strlen(str); - - if (len >= 2) + if ((len = strlen(str)) >= 2) if ((str[0] == '0') && ((str[1] == 'x') || (str[1] == 'X'))) hex_str = str + 2; @@ -543,7 +533,8 @@ int string_count_occurrences_single_character(char *str, char t) int ctr = 0; int i; - for (i = 0; str[i] != '\0'; ++i) { + for (i = 0; str[i] != '\0'; ++i) + { if (t == str[i]) ++ctr; } @@ -556,8 +547,8 @@ int string_count_occurrences_single_character(char *str, char t) */ void string_replace_whitespace_with_single_character(char *str, char t) { - - while (*str) { + while (*str) + { if (isspace(*str)) *str = t; str++; @@ -604,10 +595,9 @@ void string_remove_all_whitespace(char* str_trimmed, const char* str_untrimmed) */ int string_index_last_occurance(char *str, char t) { - const char * ret = strrchr(str, t); + const char *ret = strrchr(str, t); if (ret) return ret-str; - return -1; } @@ -616,16 +606,11 @@ int string_index_last_occurance(char *str, char t) */ int string_find_index_substring_string(const char* str1, const char* str2) { - int index; - if (str1[0] != '\0') { const char *pfound = strstr(str1, str2); - if (pfound != NULL) - { - index = (pfound - str1); - return index; - } + if (pfound) + return (pfound - str1); } return -1;