Skip to content

Commit

Permalink
Cleanups/simplifications
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Aug 17, 2023
1 parent ed4e7ab commit 24287b1
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 198 deletions.
10 changes: 5 additions & 5 deletions gfx/gfx_animation.c
Original file line number Diff line number Diff line change
Expand Up @@ -2000,18 +2000,18 @@ bool gfx_animation_line_ticker_smooth(gfx_animation_ctx_line_ticker_smooth_t *li
gfx_animation_t *p_anim = &anim_st;
const char *wideglyph_str = NULL;
int wideglyph_width = 100;
void (*word_wrap_func)(char *dst, size_t dst_size,
size_t (*word_wrap_func)(char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines);

/* Sanity check */
if (!line_ticker)
return false;

if (!line_ticker->font ||
string_is_empty(line_ticker->src_str) ||
(line_ticker->field_width < 1) ||
(line_ticker->field_height < 1))
if ( !line_ticker->font
|| string_is_empty(line_ticker->src_str)
|| (line_ticker->field_width < 1)
|| (line_ticker->field_height < 1))
goto end;

/* Get font dimensions */
Expand Down
8 changes: 4 additions & 4 deletions libretro-common/include/string/stdstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ RETRO_BEGIN_DECLS
#define TOUPPER(c) ((c) & ~(lr_char_props[(unsigned char)(c)] & 0x20))

/* C standard says \f \v are space, but this one disagrees */
#define ISSPACE(c) (lr_char_props[(unsigned char)(c)] & 0x80)
#define ISSPACE(c) (lr_char_props[(unsigned char)(c)] & 0x80)

#define ISDIGIT(c) (lr_char_props[(unsigned char)(c)] & 0x40)
#define ISALPHA(c) (lr_char_props[(unsigned char)(c)] & 0x20)
Expand Down Expand Up @@ -204,7 +204,7 @@ char *string_trim_whitespace(char *const s);
* correctly any text containing so-called 'wide' Unicode
* characters (e.g. CJK languages, emojis, etc.).
**/
void word_wrap(char *dst, size_t dst_size, const char *src, size_t src_len,
size_t word_wrap(char *dst, size_t dst_size, const char *src, size_t src_len,
int line_width, int wideglyph_width, unsigned max_lines);

/**
Expand Down Expand Up @@ -241,7 +241,7 @@ void word_wrap(char *dst, size_t dst_size, const char *src, size_t src_len,
* on-screen pixel width deviates greatly from the set
* @wideglyph_width value.
**/
void word_wrap_wideglyph(
size_t word_wrap_wideglyph(
char *dst, size_t dst_size,
const char *src, size_t src_len,
int line_width, int wideglyph_width,
Expand Down Expand Up @@ -331,7 +331,7 @@ int string_count_occurrences_single_character(const char *str, char c);

/**
* string_replace_whitespace_with_single_character:
*
*
* Leaf function.
*
* Replaces all spaces with given character @c.
Expand Down
83 changes: 31 additions & 52 deletions libretro-common/media/media_detect_cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,39 @@ static void media_zero_trailing_spaces(char *buf, size_t len)

static bool media_skip_spaces(const char **buf, size_t len)
{
bool found = false;
unsigned i;

if (!buf || !*buf || !**buf)
return false;

for (i = 0; i < len; i++)
if (buf && *buf && **buf)
{
if ((*buf)[i] == ' ' || (*buf)[i] == '\t')
continue;
size_t i;
for (i = 0; i < len; i++)
{
if ((*buf)[i] == ' ' || (*buf)[i] == '\t')
continue;

*buf += i;
found = true;
break;
*buf += i;
return true;
}
}

if (found)
return true;

return false;
}

/* Fill in "info" with detected CD info. Use this when you have a cue file and want it parsed to find the first data track and any pregap info. */
bool media_detect_cd_info_cue(const char *path, media_detect_cd_info_t *info)
{
RFILE *file = NULL;
char *line = NULL;
char track_path[PATH_MAX_LENGTH] = {0};
RFILE *file = NULL;
char *line = NULL;
char track_path[PATH_MAX_LENGTH] = {0};
char track_abs_path[PATH_MAX_LENGTH] = {0};
char track_mode[11] = {0};
bool found_file = false;
bool found_track = false;
unsigned first_data_track = 0;
uint64_t data_track_pregap_bytes = 0;
char track_mode[11] = {0};
bool found_file = false;
bool found_track = false;
unsigned first_data_track = 0;
uint64_t data_track_pregap_bytes = 0;

if (string_is_empty(path) || !info)
return false;

file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0);

if (!file)
if (!(file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0)))
{
#ifdef MEDIA_CUE_PARSE_DEBUG
printf("[MEDIA] Could not open cue path for reading: %s\n", path);
Expand Down Expand Up @@ -116,8 +108,8 @@ bool media_detect_cd_info_cue(const char *path, media_detect_cd_info_t *info)
if (!string_is_empty(file))
{
const char *file_end = NULL;
size_t file_len = 0;
bool quoted = false;
size_t file_len = 0;
bool quoted = false;

if (file[0] == '"')
{
Expand Down Expand Up @@ -198,7 +190,7 @@ bool media_detect_cd_info_cue(const char *path, media_detect_cd_info_t *info)
if (!string_is_empty(pregap))
{
media_skip_spaces(&pregap, strlen(pregap));
found_file = false;
found_file = false;
found_track = false;

if (first_data_track && !string_is_empty(track_mode))
Expand Down Expand Up @@ -273,9 +265,7 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_
if (string_is_empty(path) || !info)
return false;

file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0);

if (!file)
if (!(file = filestream_open(path, RETRO_VFS_FILE_ACCESS_READ, 0)))
{
#ifdef MEDIA_CUE_PARSE_DEBUG
printf("[MEDIA] Could not open path for reading: %s\n", path);
Expand All @@ -285,11 +275,11 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_
}

{
unsigned offset = 0;
unsigned offset = 0;
unsigned sector_size = 0;
unsigned buf_size = 17 * 2352;
char *buf = (char*)calloc(1, buf_size);
int64_t read_bytes = 0;
unsigned buf_size = 17 * 2352;
char *buf = (char*)calloc(1, buf_size);
int64_t read_bytes = 0;

if (!buf)
return false;
Expand Down Expand Up @@ -321,23 +311,14 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_
/* Assume track data contains all fields. */
sector_size = 2352;

/* Assume Mode 2 formed (formless is rarely used) */
if (buf[15] == 2)
{
/* assume Mode 2 formed (formless is rarely used) */
offset = 24;
}
else
{
/* assume Mode 1 */
offset = 16;
}
offset = 24;
else /* Assume Mode 1 */
offset = 16;
}
else
{
/* Assume sectors only contain user data instead. */
offset = 0;
else /* Assume sectors only contain user data instead. */
sector_size = 2048;
}

if (!memcmp(buf + offset, "SEGADISCSYSTEM",
STRLEN_CONST("SEGADISCSYSTEM")))
Expand Down Expand Up @@ -551,13 +532,11 @@ bool media_detect_cd_info(const char *path, uint64_t pregap_bytes, media_detect_
else if (!memcmp(buf + offset, "\x01\x5a\x5a\x5a\x5a\x5a\x01\x00\x00\x00\x00\x00", 12))
{
info->system_id = MEDIA_CD_SYSTEM_3DO;

strcpy_literal(info->system, "3DO");
}
else if (!memcmp(buf + offset + 0x950, "PC Engine CD-ROM SYSTEM", 23))
{
info->system_id = MEDIA_CD_SYSTEM_PC_ENGINE_CD;

strcpy_literal(info->system, "TurboGrafx-CD / PC-Engine CD");
}

Expand Down
4 changes: 1 addition & 3 deletions libretro-common/queues/fifo_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ static bool fifo_initialize_internal(fifo_buffer_t *buf, size_t size)

bool fifo_initialize(fifo_buffer_t *buf, size_t size)
{
if (!buf)
return false;
return fifo_initialize_internal(buf, size);
return (buf && fifo_initialize_internal(buf, size));
}

void fifo_free(fifo_buffer_t *buffer)
Expand Down
36 changes: 13 additions & 23 deletions libretro-common/queues/message_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
#include <compat/strl.h>
#include <compat/posix_string.h>

static bool msg_queue_initialize_internal(msg_queue_t *queue, size_t size)
bool msg_queue_initialize(msg_queue_t *queue, size_t size)
{
struct queue_elem **elems = (struct queue_elem**)calloc(size + 1,
sizeof(struct queue_elem*));
struct queue_elem **elems = NULL;

if (!queue)
return false;

if (!elems)
if (!(elems = (struct queue_elem**)
calloc(size + 1, sizeof(struct queue_elem*))))
return false;

queue->tmp_msg = NULL;
Expand All @@ -55,27 +58,18 @@ static bool msg_queue_initialize_internal(msg_queue_t *queue, size_t size)
**/
msg_queue_t *msg_queue_new(size_t size)
{
msg_queue_t *queue = (msg_queue_t*)malloc(sizeof(*queue));

if (!queue)
return NULL;
msg_queue_t *queue = (msg_queue_t*)malloc(sizeof(*queue));

if (!msg_queue_initialize_internal(queue, size))
if (!msg_queue_initialize(queue, size))
{
free(queue);
if (queue)
free(queue);
return NULL;
}

return queue;
}

bool msg_queue_initialize(msg_queue_t *queue, size_t size)
{
if (!queue)
return false;
return msg_queue_initialize_internal(queue, size);
}

/**
* msg_queue_free:
* @queue : pointer to queue object
Expand Down Expand Up @@ -126,9 +120,7 @@ void msg_queue_push(msg_queue_t *queue, const char *msg,
if (!queue || queue->ptr >= queue->size)
return;

new_elem = (struct queue_elem*)malloc(
sizeof(struct queue_elem));
if (!new_elem)
if (!(new_elem = (struct queue_elem*)malloc(sizeof(struct queue_elem))))
return;

new_elem->duration = duration;
Expand Down Expand Up @@ -180,7 +172,7 @@ void msg_queue_clear(msg_queue_t *queue)
queue->elems[i] = NULL;
}
}
queue->ptr = 1;
queue->ptr = 1;
free(queue->tmp_msg);
queue->tmp_msg = NULL;
}
Expand All @@ -199,8 +191,6 @@ const char *msg_queue_pull(msg_queue_t *queue)
struct queue_elem *front = NULL, *last = NULL;
size_t tmp_ptr = 1;

(void)tmp_ptr;

/* Nothing in queue. */
if (!queue || queue->ptr == 1)
return NULL;
Expand Down
25 changes: 9 additions & 16 deletions libretro-common/queues/task_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static slock_t *property_lock = NULL;
static slock_t *queue_lock = NULL;
static scond_t *worker_cond = NULL;
static sthread_t *worker_thread = NULL;
static bool worker_continue = true;
static bool worker_continue = true;
/* use running_lock when touching it */
#endif

Expand Down Expand Up @@ -132,11 +132,11 @@ static void task_queue_put(task_queue_t *queue, retro_task_t *task)

if (queue->front)
{
/* Make sure to insert in order - the queue is
/* Make sure to insert in order - the queue is
* sorted by 'when' so items that aren't scheduled
* to run immediately are at the back of the queue.
* to run immediately are at the back of the queue.
* Items with the same 'when' are inserted after
* all the other items with the same 'when'.
* all the other items with the same 'when'.
* This primarily affects items with a 'when' of 0.
*/
if (queue->back)
Expand Down Expand Up @@ -234,7 +234,7 @@ static void retro_task_regular_gather(void)
if (task->finished)
task_queue_put(&tasks_finished, task);
else
retro_task_regular_push_running(task);
task_queue_put(&tasks_running, task);
}

retro_task_internal_gather();
Expand Down Expand Up @@ -468,18 +468,14 @@ static void retro_task_threaded_retrieve(task_retriever_data_t *data)
{
/* Protect access to running tasks */
slock_lock(running_lock);

/* Call regular retrieve function */
retro_task_regular_retrieve(data);

/* Release access to running tasks */
slock_unlock(running_lock);
}

static void threaded_worker(void *userdata)
{
(void)userdata;

for (;;)
{
retro_task_t *task = NULL;
Expand Down Expand Up @@ -522,13 +518,13 @@ static void threaded_worker(void *userdata)
if (!finished)
{
/* Move the task to the back of the queue */
/* mimics retro_task_threaded_push_running,
/* mimics retro_task_threaded_push_running,
* but also includes a task_queue_remove */
slock_lock(running_lock);
slock_lock(queue_lock);

/* do nothing if only item in queue */
if (task->next)
if (task->next)
{
task_queue_remove(&tasks_running, task);
task_queue_put(&tasks_running, task);
Expand Down Expand Up @@ -734,16 +730,13 @@ void task_queue_cancel_task(void *task)

void *task_queue_retriever_info_next(task_retriever_info_t **link)
{
void *data = NULL;

/* Grab data and move to next link */
if (*link)
{
data = (*link)->data;
*link = (*link)->next;
return (*link)->data;
}

return data;
return NULL;
}

void task_queue_retriever_info_free(task_retriever_info_t *list)
Expand Down
Loading

0 comments on commit 24287b1

Please sign in to comment.