(libretro-common) Style nits/cleanups

This commit is contained in:
libretroadmin 2022-07-10 18:13:25 +02:00
parent 79fc6d3b50
commit 0e85113eb3
11 changed files with 211 additions and 327 deletions

View File

@ -373,10 +373,8 @@ static void *win32_display_server_get_resolution_list(
} }
*len = count; *len = count;
conf = (struct video_display_config*) if (!(conf = (struct video_display_config*)
calloc(*len, sizeof(struct video_display_config)); calloc(*len, sizeof(struct video_display_config))))
if (!conf)
return NULL; return NULL;
for (i = 0, j = 0; win32_get_video_output(&dm, i, sizeof(dm)); i++) for (i = 0, j = 0; win32_get_video_output(&dm, i, sizeof(dm)); i++)

View File

@ -364,16 +364,12 @@ void gfx_thumbnail_request_file(
thumbnail->status = GFX_THUMBNAIL_STATUS_MISSING; thumbnail->status = GFX_THUMBNAIL_STATUS_MISSING;
/* Check if file path is valid */ /* Check if file path is valid */
if (string_is_empty(file_path)) if ( string_is_empty(file_path)
return; || !path_is_valid(file_path))
if (!path_is_valid(file_path))
return; return;
/* Load thumbnail */ /* Load thumbnail */
thumbnail_tag = (gfx_thumbnail_tag_t*)malloc(sizeof(gfx_thumbnail_tag_t)); if (!(thumbnail_tag = (gfx_thumbnail_tag_t*)malloc(sizeof(gfx_thumbnail_tag_t))))
if (!thumbnail_tag)
return; return;
/* Configure user data */ /* Configure user data */
@ -788,17 +784,22 @@ void gfx_thumbnail_get_draw_dimensions(
unsigned width, unsigned height, float scale_factor, unsigned width, unsigned height, float scale_factor,
float *draw_width, float *draw_height) float *draw_width, float *draw_height)
{ {
video_driver_state_t *video_st = video_state_get_ptr(); float core_aspect;
float display_aspect; float display_aspect;
float thumbnail_aspect; float thumbnail_aspect;
float core_aspect; video_driver_state_t *video_st = video_state_get_ptr();
/* Sanity check */ /* Sanity check */
if (!thumbnail || (width < 1) || (height < 1)) if ( !thumbnail
goto error; || (width < 1)
|| (height < 1)
if ((thumbnail->width < 1) || (thumbnail->height < 1)) || (thumbnail->width < 1)
goto error; || (thumbnail->height < 1))
{
*draw_width = 0.0f;
*draw_height = 0.0f;
return;
}
/* Account for display/thumbnail/core aspect ratio /* Account for display/thumbnail/core aspect ratio
* differences */ * differences */
@ -842,11 +843,6 @@ void gfx_thumbnail_get_draw_dimensions(
* without scaling manually... */ * without scaling manually... */
*draw_width *= scale_factor; *draw_width *= scale_factor;
*draw_height *= scale_factor; *draw_height *= scale_factor;
return;
error:
*draw_width = 0.0f;
*draw_height = 0.0f;
} }
/* Draws specified thumbnail with specified alignment /* Draws specified thumbnail with specified alignment
@ -872,10 +868,14 @@ void gfx_thumbnail_draw(
gfx_display_t *p_disp = disp_get_ptr(); gfx_display_t *p_disp = disp_get_ptr();
gfx_display_ctx_driver_t *dispctx = p_disp->dispctx; gfx_display_ctx_driver_t *dispctx = p_disp->dispctx;
/* Sanity check */ /* Sanity check */
if (!thumbnail || if (
(width < 1) || (height < 1) || (alpha <= 0.0f) || (scale_factor <= 0.0f)) !thumbnail
return; || !dispctx
if (!dispctx) || (width < 1)
|| (height < 1)
|| (alpha <= 0.0f)
|| (scale_factor <= 0.0f)
)
return; return;
/* Only draw thumbnail if it is available... */ /* Only draw thumbnail if it is available... */

View File

@ -255,10 +255,9 @@ static bool utf16_to_char(uint8_t **utf_data,
utf16_conv_utf8(NULL, dest_len, in, len); utf16_conv_utf8(NULL, dest_len, in, len);
*dest_len += 1; *dest_len += 1;
*utf_data = (uint8_t*)malloc(*dest_len); *utf_data = (uint8_t*)malloc(*dest_len);
if (*utf_data == 0) if (*utf_data != 0)
return false; return utf16_conv_utf8(*utf_data, dest_len, in, len);
return false;
return utf16_conv_utf8(*utf_data, dest_len, in, len);
} }
bool utf16_to_char_string(const uint16_t *in, char *s, size_t len) 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) if (!path_buf_wide_len)
return strdup(str); return strdup(str);
path_buf_wide = (wchar_t*) if ((path_buf_wide = (wchar_t*)
calloc(path_buf_wide_len + sizeof(wchar_t), sizeof(wchar_t)); calloc(path_buf_wide_len + sizeof(wchar_t), sizeof(wchar_t))))
if (path_buf_wide)
{ {
MultiByteToWideChar(cp_in, 0, MultiByteToWideChar(cp_in, 0,
str, -1, path_buf_wide, path_buf_wide_len); 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. */ /* Returned pointer MUST be freed by the caller if non-NULL. */
char* utf8_to_local_string_alloc(const char *str) char* utf8_to_local_string_alloc(const char *str)
{ {
if (str && *str)
{
#if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE) #if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE)
if (str && *str)
return mb_to_mb_string_alloc(str, CODEPAGE_UTF8, CODEPAGE_LOCAL); return mb_to_mb_string_alloc(str, CODEPAGE_UTF8, CODEPAGE_LOCAL);
#else #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); return strdup(str);
#endif #endif
}
return NULL; return NULL;
} }
/* Returned pointer MUST be freed by the caller if non-NULL. */ /* Returned pointer MUST be freed by the caller if non-NULL. */
char* local_to_utf8_string_alloc(const char *str) char* local_to_utf8_string_alloc(const char *str)
{ {
if (str && *str)
{
#if defined(_WIN32) && !defined(_XBOX) && !defined(UNICODE) #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 #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); return strdup(str);
#endif #endif
} return NULL;
return NULL;
} }
/* Returned pointer MUST be freed by the caller if non-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 #ifdef _WIN32
int len = 0; int len = 0;
int out_len = 0;
#else #else
size_t len = 0; size_t len = 0;
size_t out_len = 0;
#endif #endif
wchar_t *buf = NULL; wchar_t *buf = NULL;
@ -393,56 +386,44 @@ wchar_t* utf8_to_utf16_string_alloc(const char *str)
return NULL; return NULL;
#ifdef _WIN32 #ifdef _WIN32
len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0); if ((len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)))
if (len)
{ {
buf = (wchar_t*)calloc(len, sizeof(wchar_t)); if (!(buf = (wchar_t*)calloc(len, sizeof(wchar_t))))
if (!buf)
return NULL; 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 else
{ {
/* fallback to ANSI codepage instead */ /* Fallback to ANSI codepage instead */
len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0); if ((len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0)))
if (len)
{ {
buf = (wchar_t*)calloc(len, sizeof(wchar_t)); if (!(buf = (wchar_t*)calloc(len, sizeof(wchar_t))))
if (!buf)
return NULL; 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 #else
/* NOTE: For now, assume non-Windows platforms' locale is already UTF-8. */ /* NOTE: For now, assume non-Windows platforms' locale is already UTF-8. */
len = mbstowcs(NULL, str, 0) + 1; if ((len = mbstowcs(NULL, str, 0) + 1))
if (len)
{ {
buf = (wchar_t*)calloc(len, sizeof(wchar_t)); if (!(buf = (wchar_t*)calloc(len, sizeof(wchar_t))))
if (!buf)
return NULL; return NULL;
out_len = mbstowcs(buf, str, len); if ((mbstowcs(buf, str, len)) == (size_t)-1)
} {
free(buf);
if (out_len == (size_t)-1) return NULL;
{ }
free(buf);
return NULL;
} }
#endif #endif
@ -465,20 +446,17 @@ char* utf16_to_utf8_string_alloc(const wchar_t *str)
#ifdef _WIN32 #ifdef _WIN32
{ {
UINT code_page = CP_UTF8; UINT code_page = CP_UTF8;
len = WideCharToMultiByte(code_page,
0, str, -1, NULL, 0, NULL, NULL);
/* fallback to ANSI codepage instead */ /* fallback to ANSI codepage instead */
if (!len) if (!(len = WideCharToMultiByte(code_page,
0, str, -1, NULL, 0, NULL, NULL)))
{ {
code_page = CP_ACP; code_page = CP_ACP;
len = WideCharToMultiByte(code_page, len = WideCharToMultiByte(code_page,
0, str, -1, NULL, 0, NULL, NULL); 0, str, -1, NULL, 0, NULL, NULL);
} }
buf = (char*)calloc(len, sizeof(char)); if (!(buf = (char*)calloc(len, sizeof(char))))
if (!buf)
return NULL; return NULL;
if (WideCharToMultiByte(code_page, if (WideCharToMultiByte(code_page,
@ -491,13 +469,9 @@ char* utf16_to_utf8_string_alloc(const wchar_t *str)
#else #else
/* NOTE: For now, assume non-Windows platforms' /* NOTE: For now, assume non-Windows platforms'
* locale is already UTF-8. */ * locale is already UTF-8. */
len = wcstombs(NULL, str, 0) + 1; if ((len = wcstombs(NULL, str, 0) + 1))
if (len)
{ {
buf = (char*)calloc(len, sizeof(char)); if (!(buf = (char*)calloc(len, sizeof(char))))
if (!buf)
return NULL; return NULL;
if (wcstombs(buf, str, len) == (size_t)-1) if (wcstombs(buf, str, len) == (size_t)-1)

View File

@ -521,17 +521,16 @@ static bool config_file_parse_line(config_file_t *conf,
if (comment) if (comment)
{ {
config_file_t sub_conf; config_file_t sub_conf;
bool include_found = false;
bool reference_found = false;
char real_path[PATH_MAX_LENGTH]; char real_path[PATH_MAX_LENGTH];
char *path = NULL; char *path = NULL;
char *include_line = NULL; char *include_line = NULL;
char *reference_line = NULL; char *reference_line = NULL;
bool include_found = string_starts_with_size(comment,
include_found = string_starts_with_size(comment, "include ", "include ",
STRLEN_CONST("include ")); STRLEN_CONST("include "));
reference_found = string_starts_with_size(comment, "reference ", bool reference_found = string_starts_with_size(comment,
STRLEN_CONST("reference ")); "reference ",
STRLEN_CONST("reference "));
/* All comments except those starting with the include or /* All comments except those starting with the include or
* reference directive are ignored */ * reference directive are ignored */
@ -547,9 +546,7 @@ static bool config_file_parse_line(config_file_t *conf,
if (string_is_empty(include_line)) if (string_is_empty(include_line))
return false; return false;
path = config_file_extract_value(include_line, false); if (!(path = config_file_extract_value(include_line, false)))
if (!path)
return false; return false;
if ( string_is_empty(path) 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)) if (string_is_empty(reference_line))
return false; return false;
path = config_file_extract_value(reference_line, false); if (!(path = config_file_extract_value(reference_line, false)))
if (!path)
return false; return false;
config_file_add_reference(conf, path); config_file_add_reference(conf, path);
@ -610,8 +605,7 @@ static bool config_file_parse_line(config_file_t *conf,
line++; line++;
/* Allocate storage for key */ /* Allocate storage for key */
key = (char*)malloc(cur_size + 1); if (!(key = (char*)malloc(cur_size + 1)))
if (!key)
return false; return false;
/* Copy line contents into key until we /* 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) if (idx == cur_size)
{ {
cur_size *= 2; cur_size *= 2;
key_tmp = (char*)realloc(key, cur_size + 1); if (!(key_tmp = (char*)realloc(key, cur_size + 1)))
if (!key_tmp)
{ {
free(key); free(key);
return false; return false;
} }
key = key_tmp; key = key_tmp;
} }
key[idx++] = *line++; key[idx++] = *line++;
@ -640,10 +632,9 @@ static bool config_file_parse_line(config_file_t *conf,
/* Add key and value entries to list */ /* Add key and value entries to list */
list->key = key; list->key = key;
list->value = config_file_extract_value(line, true);
/* An entry without a value is invalid */ /* An entry without a value is invalid */
if (!list->value) if (!(list->value = config_file_extract_value(line, true)))
{ {
list->key = NULL; list->key = NULL;
free(key); free(key);
@ -768,9 +759,8 @@ bool config_file_deinitialize(config_file_t *conf)
void config_file_free(config_file_t *conf) void config_file_free(config_file_t *conf)
{ {
if (!config_file_deinitialize(conf)) if (config_file_deinitialize(conf))
return; free(conf);
free(conf);
} }
bool config_append_file(config_file_t *conf, const char *path) 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) const char *path)
{ {
struct config_file *conf = config_file_new_alloc(); struct config_file *conf = config_file_new_alloc();
if ( conf
if (!conf) && config_file_from_string_internal(
return NULL; conf, from_string, path) != -1)
if (config_file_from_string_internal(conf, from_string, path) == -1) return conf;
{ if (conf)
config_file_free(conf); config_file_free(conf);
return NULL; return NULL;
}
return conf;
} }
config_file_t *config_file_new_from_path_to_string(const char *path) 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)) if (path_is_valid(path))
{ {
uint8_t *ret_buf = NULL;
int64_t length = 0;
if (filestream_read_file(path, (void**)&ret_buf, &length)) if (filestream_read_file(path, (void**)&ret_buf, &length))
{ {
config_file_t *conf = NULL;
/* Note: 'ret_buf' is not used outside this /* Note: 'ret_buf' is not used outside this
* function - we do not care that it will be * function - we do not care that it will be
* modified by config_file_new_from_string() */ * 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) if ((void*)ret_buf)
free((void*)ret_buf); free((void*)ret_buf);
return conf;
} }
} }
return conf; return NULL;
} }
config_file_t *config_file_new_with_callback( 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(); struct config_file *conf = config_file_new_alloc();
if (!path || !*path) if (!path || !*path)
return conf; return conf;
ret = config_file_load_internal(conf, path, 0, cb); if ((ret = config_file_load_internal(conf, path, 0, cb)) == -1)
if (ret == -1)
{ {
config_file_free(conf); config_file_free(conf);
return NULL; return NULL;
@ -874,8 +862,7 @@ config_file_t *config_file_new(const char *path)
struct config_file *conf = config_file_new_alloc(); struct config_file *conf = config_file_new_alloc();
if (!path || !*path) if (!path || !*path)
return conf; return conf;
ret = config_file_load_internal(conf, path, 0, NULL); if ((ret = config_file_load_internal(conf, path, 0, NULL)) == -1)
if (ret == -1)
{ {
config_file_free(conf); config_file_free(conf);
return NULL; return NULL;
@ -918,16 +905,14 @@ static struct config_entry_list *config_get_entry_internal(
const config_file_t *conf, const config_file_t *conf,
const char *key, struct config_entry_list **prev) const char *key, struct config_entry_list **prev)
{ {
struct config_entry_list *entry = NULL; struct config_entry_list *entry = RHMAP_GET_STR(conf->entries_map, key);
struct config_entry_list *previous = prev ? *prev : NULL;
entry = RHMAP_GET_STR(conf->entries_map, key);
if (entry) if (entry)
return entry; return entry;
if (prev) if (prev)
{ {
struct config_entry_list *previous = *prev;
for (entry = conf->entries; entry; entry = entry->next) for (entry = conf->entries; entry; entry = entry->next)
previous = entry; 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) bool config_get_config_path(config_file_t *conf, char *s, size_t len)
{ {
if (!conf) if (conf)
return false; return strlcpy(s, conf->path, len);
return strlcpy(s, conf->path, len); return false;
} }
bool config_get_array(config_file_t *conf, const char *key, 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; return;
last = conf->entries; last = conf->entries;
entry = config_get_entry_internal(conf, key, &last);
if (!entry) if (!(entry = config_get_entry_internal(conf, key, &last)))
return; return;
(void)RHMAP_DEL_STR(conf->entries_map, entry->key); (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) if (!file)
return false; return false;
buf = calloc(1, 0x4000); buf = calloc(1, 0x4000);
setvbuf(file, (char*)buf, _IOFBF, 0x4000); setvbuf(file, (char*)buf, _IOFBF, 0x4000);
config_file_dump(conf, file, sort); config_file_dump(conf, file, sort);

View File

@ -254,10 +254,9 @@ bool path_is_compressed_file(const char* path)
{ {
const char *ext = path_get_extension(path); const char *ext = path_get_extension(path);
if (!string_is_empty(ext)) if (!string_is_empty(ext))
if ( string_is_equal_noncase(ext, "zip") || return ( string_is_equal_noncase(ext, "zip")
string_is_equal_noncase(ext, "apk") || || string_is_equal_noncase(ext, "apk")
string_is_equal_noncase(ext, "7z")) || string_is_equal_noncase(ext, "7z"));
return true;
return false; return false;
} }
@ -457,7 +456,6 @@ void fill_pathname_basedir_noext(char *out_dir,
bool fill_pathname_parent_dir_name(char *out_dir, bool fill_pathname_parent_dir_name(char *out_dir,
const char *in_dir, size_t size) const char *in_dir, size_t size)
{ {
bool success = false;
char *temp = strdup(in_dir); char *temp = strdup(in_dir);
char *last = find_last_slash(temp); char *last = find_last_slash(temp);
@ -473,26 +471,24 @@ bool fill_pathname_parent_dir_name(char *out_dir,
*last = '\0'; *last = '\0';
/* Point in_dir to the address of the last slash. */ /* 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, /* If find_last_slash returns NULL, it means there was no slash in temp,
so use temp as-is. */ so use temp as-is. */
if (!in_dir) if (!(in_dir = find_last_slash(temp)))
in_dir = temp; in_dir = temp;
success = in_dir && in_dir[1]; if (in_dir && in_dir[1])
if (success)
{ {
/* If path starts with an slash, eliminate it. */ /* If path starts with an slash, eliminate it. */
if (path_is_absolute(in_dir)) if (path_is_absolute(in_dir))
strlcpy(out_dir, in_dir + 1, size); strlcpy(out_dir, in_dir + 1, size);
else else
strlcpy(out_dir, in_dir, size); strlcpy(out_dir, in_dir, size);
free(temp);
return true;
} }
free(temp); free(temp);
return success; return false;
} }
/** /**
@ -591,9 +587,7 @@ void path_basedir(char *path)
if (strlen(path) < 2) if (strlen(path) < 2)
return; return;
last = find_last_slash(path); if ((last = find_last_slash(path)))
if (last)
last[1] = '\0'; last[1] = '\0';
else else
strlcpy(path, "." PATH_DEFAULT_SLASH(), 3); strlcpy(path, "." PATH_DEFAULT_SLASH(), 3);
@ -690,15 +684,13 @@ bool path_is_absolute(const char *path)
#if defined(_WIN32) #if defined(_WIN32)
/* Many roads lead to Rome... /* Many roads lead to Rome...
* Note: Drive letter can only be 1 character long */ * Note: Drive letter can only be 1 character long */
if (string_starts_with_size(path, "\\\\", STRLEN_CONST("\\\\")) || return ( string_starts_with_size(path, "\\\\", STRLEN_CONST("\\\\"))
string_starts_with_size(path + 1, ":/", STRLEN_CONST(":/")) || || string_starts_with_size(path + 1, ":/", STRLEN_CONST(":/"))
string_starts_with_size(path + 1, ":\\", STRLEN_CONST(":\\"))) || string_starts_with_size(path + 1, ":\\", STRLEN_CONST(":\\")));
return true;
#elif defined(__wiiu__) || defined(VITA) #elif defined(__wiiu__) || defined(VITA)
{ {
const char *seperator = strchr(path, ':'); const char *seperator = strchr(path, ':');
if (seperator && (seperator[1] == '/')) return (seperator && (seperator[1] == '/'));
return true;
} }
#endif #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 */ * if there are no slashes, they point relative to where one would be */
do do
{ {
next = strchr(p, '/'); if (!(next = strchr(p, '/')))
if (!next)
next = buf_end; next = buf_end;
if ((next - p == 2 && p[0] == '.' && p[1] == '.')) if ((next - p == 2 && p[0] == '.' && p[1] == '.'))

View File

@ -118,10 +118,8 @@ bool path_mkdir(const char *dir)
/* Use heap. Real chance of stack /* Use heap. Real chance of stack
* overflow if we recurse too hard. */ * overflow if we recurse too hard. */
basedir = strdup(dir); if (!(basedir = strdup(dir)))
return false;
if (!basedir)
return false;
path_parent_dir(basedir); path_parent_dir(basedir);

View File

@ -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) if (nitems < list->capacity || nitems > (size_t)-1/item_size)
return false; return false;
new_data = (struct item_file*)realloc(list->list, nitems * item_size); if (!(new_data = (struct item_file*)realloc(list->list, nitems * item_size)))
if (!new_data)
return false; return false;
memset(&new_data[list->capacity], 0, item_size * (nitems - list->capacity)); 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; continue;
} }
str = (const char *)strcasestr(alt, needle); if ((str = (const char *)strcasestr(alt, needle)) == alt)
if (str == alt)
{ {
/* Found match with first chars, best possible match. */ /* Found match with first chars, best possible match. */
*idx = i; *idx = i;

View File

@ -182,8 +182,7 @@ bool string_list_append(struct string_list *list, const char *elem,
(list->cap > 0) ? (list->cap * 2) : 32)) (list->cap > 0) ? (list->cap * 2) : 32))
return false; return false;
data_dup = strdup(elem); if (!(data_dup = strdup(elem)))
if (!data_dup)
return false; return false;
list->elems[list->size].data = data_dup; 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)) !string_list_capacity(list, list->cap * 2))
return false; return false;
data_dup = (char*)malloc(length + 1); if (!(data_dup = (char*)malloc(length + 1)))
if (!data_dup)
return false; return false;
strlcpy(data_dup, elem, length + 1); strlcpy(data_dup, elem, length + 1);
@ -298,8 +295,7 @@ struct string_list *string_split(const char *str, const char *delim)
if (!list) if (!list)
return NULL; return NULL;
copy = strdup(str); if (!(copy = strdup(str)))
if (!copy)
goto error; goto error;
tmp = strtok_r(copy, delim, &save); tmp = strtok_r(copy, delim, &save);
@ -334,8 +330,7 @@ bool string_split_noalloc(struct string_list *list,
if (!list) if (!list)
return false; return false;
copy = strdup(str); if (!(copy = strdup(str)))
if (!copy)
return false; return false;
tmp = strtok_r(copy, delim, &save); tmp = strtok_r(copy, delim, &save);
@ -377,15 +372,13 @@ struct string_list *string_separate(char *str, const char *delim)
/* Sanity check */ /* Sanity check */
if (!str || string_is_empty(delim)) if (!str || string_is_empty(delim))
goto error; return NULL;
if (!(list = string_list_new()))
return NULL;
str_ptr = &str; 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) while (token)
{ {
union string_list_elem_attr attr; union string_list_elem_attr attr;
@ -393,22 +386,17 @@ struct string_list *string_separate(char *str, const char *delim)
attr.i = 0; attr.i = 0;
if (!string_list_append(list, token, attr)) if (!string_list_append(list, token, attr))
goto error; {
free(token);
string_list_free(list);
return NULL;
}
free(token); free(token);
token = NULL;
token = string_tokenize(str_ptr, delim); token = string_tokenize(str_ptr, delim);
} }
return list; return list;
error:
if (token)
free(token);
if (list)
string_list_free(list);
return NULL;
} }
bool string_separate_noalloc( 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) int string_list_find_elem(const struct string_list *list, const char *elem)
{ {
size_t i; if (list)
if (!list)
return false;
for (i = 0; i < list->size; i++)
{ {
if (string_is_equal_noncase(list->elems[i].data, elem)) size_t i;
return (int)(i + 1); for (i = 0; i < list->size; i++)
{
if (string_is_equal_noncase(list->elems[i].data, elem))
return (int)(i + 1);
}
} }
return false; return false;
@ -496,16 +483,15 @@ bool string_list_find_elem_prefix(const struct string_list *list,
for (i = 0; i < list->size; i++) for (i = 0; i < list->size; i++)
{ {
if (string_is_equal_noncase(list->elems[i].data, elem) || if ( string_is_equal_noncase(list->elems[i].data, elem)
string_is_equal_noncase(list->elems[i].data, prefixed)) || string_is_equal_noncase(list->elems[i].data, prefixed))
return true; return true;
} }
return false; return false;
} }
struct string_list *string_list_clone( struct string_list *string_list_clone(const struct string_list *src)
const struct string_list *src)
{ {
unsigned i; unsigned i;
struct string_list_elem struct string_list_elem
@ -519,14 +505,13 @@ struct string_list *string_list_clone(
dest->elems = NULL; dest->elems = NULL;
dest->size = src->size; dest->size = src->size;
dest->cap = src->cap; if (src->cap < dest->size)
if (dest->cap < dest->size)
dest->cap = dest->size; dest->cap = dest->size;
else
dest->cap = src->cap;
elems = (struct string_list_elem*) if (!(elems = (struct string_list_elem*)
calloc(dest->cap, sizeof(struct string_list_elem)); calloc(dest->cap, sizeof(struct string_list_elem))))
if (!elems)
{ {
free(dest); free(dest);
return NULL; return NULL;
@ -536,11 +521,11 @@ struct string_list *string_list_clone(
for (i = 0; i < src->size; i++) for (i = 0; i < src->size; i++)
{ {
const char *_src = src->elems[i].data; const char *_src = src->elems[i].data;
size_t len = _src ? strlen(_src) : 0; size_t len = _src ? strlen(_src) : 0;
dest->elems[i].data = NULL; dest->elems[i].data = NULL;
dest->elems[i].attr = src->elems[i].attr; dest->elems[i].attr = src->elems[i].attr;
if (len != 0) if (len != 0)
{ {

View File

@ -323,17 +323,15 @@ static struct retro_task_impl impl_regular = {
static void task_queue_remove(task_queue_t *queue, retro_task_t *task) static void task_queue_remove(task_queue_t *queue, retro_task_t *task)
{ {
retro_task_t *t = NULL; retro_task_t *t = NULL;
retro_task_t *front = NULL; retro_task_t *front = queue->front;
front = queue->front;
/* Remove first element if needed */ /* Remove first element if needed */
if (task == front) if (task == front)
{ {
queue->front = task->next; queue->front = task->next;
if (queue->back == task) /* if only element, also update back */ if (queue->back == task) /* if only element, also update back */
queue->back = NULL; queue->back = NULL;
task->next = NULL; task->next = NULL;
return; return;
} }
@ -485,8 +483,7 @@ static void threaded_worker(void *userdata)
slock_lock(running_lock); slock_lock(running_lock);
/* Get first task to run */ /* Get first task to run */
task = tasks_running.front; if (!(task = tasks_running.front))
if (!task)
{ {
scond_wait(worker_cond, running_lock); scond_wait(worker_cond, running_lock);
slock_unlock(running_lock); slock_unlock(running_lock);

View File

@ -193,9 +193,11 @@ sthread_t *sthread_create_with_priority(void (*thread_func)(void*), void *userda
if (!thread) if (!thread)
return NULL; return NULL;
data = (struct thread_data*)malloc(sizeof(*data)); if (!(data = (struct thread_data*)malloc(sizeof(*data))))
if (!data) {
goto error; free(thread);
return NULL;
}
data->func = thread_func; data->func = thread_func;
data->userdata = userdata; data->userdata = userdata;
@ -242,10 +244,7 @@ sthread_t *sthread_create_with_priority(void (*thread_func)(void*), void *userda
if (thread_created) if (thread_created)
return thread; return thread;
free(data);
error:
if (data)
free(data);
free(thread); free(thread);
return NULL; return NULL;
} }
@ -307,14 +306,10 @@ void sthread_join(sthread_t *thread)
*/ */
bool sthread_isself(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 #ifdef USE_WIN32_THREADS
return GetCurrentThreadId() == thread->id; return thread ? GetCurrentThreadId() == thread->id : false;
#else #else
return pthread_equal(pthread_self(),thread->id); return thread ? pthread_equal(pthread_self(), thread->id) : false;
#endif #endif
} }
#endif #endif
@ -329,27 +324,19 @@ bool sthread_isself(sthread_t *thread)
**/ **/
slock_t *slock_new(void) slock_t *slock_new(void)
{ {
bool mutex_created = false;
slock_t *lock = (slock_t*)calloc(1, sizeof(*lock)); slock_t *lock = (slock_t*)calloc(1, sizeof(*lock));
if (!lock) if (!lock)
return NULL; return NULL;
#ifdef USE_WIN32_THREADS #ifdef USE_WIN32_THREADS
InitializeCriticalSection(&lock->lock); InitializeCriticalSection(&lock->lock);
mutex_created = true;
#else #else
mutex_created = (pthread_mutex_init(&lock->lock, NULL) == 0); if (pthread_mutex_init(&lock->lock, NULL) != 0)
{
free(lock);
return NULL;
}
#endif #endif
if (!mutex_created)
goto error;
return lock; return lock;
error:
free(lock);
return NULL;
} }
/** /**
@ -399,12 +386,10 @@ void slock_lock(slock_t *lock)
**/ **/
bool slock_try_lock(slock_t *lock) bool slock_try_lock(slock_t *lock)
{ {
if (!lock)
return false;
#ifdef USE_WIN32_THREADS #ifdef USE_WIN32_THREADS
return TryEnterCriticalSection(&lock->lock); return lock && TryEnterCriticalSection(&lock->lock);
#else #else
return pthread_mutex_trylock(&lock->lock)==0; return lock && (pthread_mutex_trylock(&lock->lock) == 0);
#endif #endif
} }
@ -467,11 +452,9 @@ scond_t *scond_new(void)
* *
* Note: We might could simplify this using vista+ condition variables, * Note: We might could simplify this using vista+ condition variables,
* but we wanted an XP compatible solution. */ * but we wanted an XP compatible solution. */
cond->event = CreateEvent(NULL, FALSE, FALSE, NULL); if (!(cond->event = CreateEvent(NULL, FALSE, FALSE, NULL)))
if (!cond->event)
goto error; goto error;
cond->hot_potato = CreateEvent(NULL, FALSE, FALSE, NULL); if (!(cond->hot_potato = CreateEvent(NULL, FALSE, FALSE, NULL)))
if (!cond->hot_potato)
{ {
CloseHandle(cond->event); CloseHandle(cond->event);
goto error; goto error;
@ -525,7 +508,6 @@ static bool _scond_wait_win32(scond_t *cond, slock_t *lock, DWORD dwMilliseconds
static bool beginPeriod = false; static bool beginPeriod = false;
DWORD tsBegin; DWORD tsBegin;
#endif #endif
DWORD waitResult; DWORD waitResult;
DWORD dwFinalTimeout = dwMilliseconds; /* Careful! in case we begin in the head, DWORD dwFinalTimeout = dwMilliseconds; /* Careful! in case we begin in the head,
we don't do the hot potato stuff, 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) if (performanceCounterFrequency.QuadPart == 0)
{
QueryPerformanceFrequency(&performanceCounterFrequency); QueryPerformanceFrequency(&performanceCounterFrequency);
}
#else #else
if (!beginPeriod) 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 */ /* walk to the end of the linked list */
while (*ptr) while (*ptr)
ptr = &((*ptr)->next); ptr = &((*ptr)->next);
*ptr = &myentry; *ptr = &myentry;
myentry.next = NULL; myentry.next = NULL;
cond->waiters++; cond->waiters++;
@ -746,18 +726,17 @@ void scond_wait(scond_t *cond, slock_t *lock)
int scond_broadcast(scond_t *cond) int scond_broadcast(scond_t *cond)
{ {
#ifdef USE_WIN32_THREADS #ifdef USE_WIN32_THREADS
/* remember: we currently have mutex */ /* Remember, we currently have mutex */
if (cond->waiters == 0) if (cond->waiters != 0)
return 0; {
/* Awaken everything which is currently queued up */
/* awaken everything which is currently queued up */ if (cond->wakens == 0)
if (cond->wakens == 0) SetEvent(cond->event);
SetEvent(cond->event); cond->wakens = cond->waiters;
cond->wakens = cond->waiters;
/* Since there is now at least one pending waken, the potato must be in play */
SetEvent(cond->hot_potato);
/* Since there is now at least one pending waken, the potato must be in play */
SetEvent(cond->hot_potato);
}
return 0; return 0;
#else #else
return pthread_cond_broadcast(&cond->cond); 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 0 timeout clearly wants immediate timeout.
* Someone asking for a 1 timeout clearly wants an actual timeout * Someone asking for a 1 timeout clearly wants an actual timeout
* of the minimum length */ * 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. /* 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. * 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 * 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) if (timeout_us == 0)
return false; return false;
else if (timeout_us < 1000) else if (timeout_us < 1000)
dwMilliseconds = 1; return _scond_wait_win32(cond, lock, 1);
/* Someone asking for 1000 or 1001 timeout shouldn't
return _scond_wait_win32(cond,lock,dwMilliseconds); * accidentally get 2ms. */
return _scond_wait_win32(cond, lock, timeout_us / 1000);
#else #else
int ret; int ret;
int64_t seconds, remainder; 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) uintptr_t sthread_get_thread_id(sthread_t *thread)
{ {
if (!thread) if (thread)
return 0; return (uintptr_t)thread->id;
return (uintptr_t)thread->id; return 0;
} }
uintptr_t sthread_get_current_thread_id(void) uintptr_t sthread_get_current_thread_id(void)

View File

@ -114,9 +114,8 @@ char *string_replace_substring(const char *in,
} }
outlen = strlen(in) - pattern_len*numhits + replacement_len*numhits; outlen = strlen(in) - pattern_len*numhits + replacement_len*numhits;
out = (char *)malloc(outlen+1);
if (!out) if (!(out = (char *)malloc(outlen+1)))
return NULL; return NULL;
outat = out; 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') while (*src != '\0')
{ {
unsigned char_len; unsigned char_len = (unsigned)(utf8skip(src, 1) - src);
char_len = (unsigned)(utf8skip(src, 1) - src);
counter_normalized += 100; counter_normalized += 100;
/* Prevent buffer overflow */ /* Prevent buffer overflow */
@ -315,7 +312,7 @@ void word_wrap_wideglyph(char *dst, size_t dst_size, const char *src, int line_w
break; break;
if (*src == ' ') if (*src == ' ')
lastspace = dst; /* Remember the location of the whitespace */ lastspace = dst; /* Remember the location of the whitespace */
else if (*src == '\n') else if (*src == '\n')
{ {
/* If newlines embedded in the input, /* If newlines embedded in the input,
@ -419,25 +416,20 @@ char* string_tokenize(char **str, const char *delim)
if (!str || string_is_empty(delim)) if (!str || string_is_empty(delim))
return NULL; return NULL;
str_ptr = *str;
/* Note: we don't check string_is_empty() here, /* Note: we don't check string_is_empty() here,
* empty strings are valid */ * empty strings are valid */
if (!str_ptr) if (!(str_ptr = *str))
return NULL; return NULL;
/* Search for delimiter */ /* Search for delimiter */
delim_ptr = strstr(str_ptr, delim); if ((delim_ptr = strstr(str_ptr, delim)))
if (delim_ptr)
token_len = delim_ptr - str_ptr; token_len = delim_ptr - str_ptr;
else else
token_len = strlen(str_ptr); token_len = strlen(str_ptr);
/* Allocate token string */ /* Allocate token string */
token = (char *)malloc((token_len + 1) * sizeof(char)); if (!(token = (char *)malloc((token_len + 1) * sizeof(char))))
if (!token)
return NULL; return NULL;
/* Copy token */ /* Copy token */
@ -475,13 +467,13 @@ void string_remove_all_chars(char *str, char c)
* with character 'replace' */ * with character 'replace' */
void string_replace_all_chars(char *str, char find, char 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. /* Converts string to unsigned integer.
@ -515,9 +507,7 @@ unsigned string_hex_to_unsigned(const char *str)
return 0; return 0;
/* Remove leading '0x', if required */ /* Remove leading '0x', if required */
len = strlen(str); if ((len = strlen(str)) >= 2)
if (len >= 2)
if ((str[0] == '0') && if ((str[0] == '0') &&
((str[1] == 'x') || (str[1] == 'X'))) ((str[1] == 'x') || (str[1] == 'X')))
hex_str = str + 2; hex_str = str + 2;
@ -543,7 +533,8 @@ int string_count_occurrences_single_character(char *str, char t)
int ctr = 0; int ctr = 0;
int i; int i;
for (i = 0; str[i] != '\0'; ++i) { for (i = 0; str[i] != '\0'; ++i)
{
if (t == str[i]) if (t == str[i])
++ctr; ++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) void string_replace_whitespace_with_single_character(char *str, char t)
{ {
while (*str)
while (*str) { {
if (isspace(*str)) if (isspace(*str))
*str = t; *str = t;
str++; 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) int string_index_last_occurance(char *str, char t)
{ {
const char * ret = strrchr(str, t); const char *ret = strrchr(str, t);
if (ret) if (ret)
return ret-str; return ret-str;
return -1; 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 string_find_index_substring_string(const char* str1, const char* str2)
{ {
int index;
if (str1[0] != '\0') if (str1[0] != '\0')
{ {
const char *pfound = strstr(str1, str2); const char *pfound = strstr(str1, str2);
if (pfound != NULL) if (pfound)
{ return (pfound - str1);
index = (pfound - str1);
return index;
}
} }
return -1; return -1;