From 71708f8dddd4dc35a8c59f7468b1a191fd0da31e Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 24 Aug 2020 12:04:56 +0200 Subject: [PATCH] (libretro-common) Add msg_queue_initialize/msg_queue_deinitialize --- .../include/queues/message_queue.h | 22 +++++- libretro-common/queues/message_queue.c | 69 ++++++++++--------- retroarch.c | 30 ++++---- 3 files changed, 72 insertions(+), 49 deletions(-) diff --git a/libretro-common/include/queues/message_queue.h b/libretro-common/include/queues/message_queue.h index 5d0e6e59b5..d79c064604 100644 --- a/libretro-common/include/queues/message_queue.h +++ b/libretro-common/include/queues/message_queue.h @@ -43,7 +43,23 @@ enum message_queue_category MESSAGE_QUEUE_CATEGORY_SUCCESS }; -typedef struct msg_queue msg_queue_t; +typedef struct queue_elem +{ + char *msg; + char *title; + unsigned duration; + unsigned prio; + enum message_queue_icon icon; + enum message_queue_category category; +} queue_elem_t; + +typedef struct msg_queue +{ + char *tmp_msg; + queue_elem_t **elems; + size_t ptr; + size_t size; +} msg_queue_t; typedef struct { @@ -66,6 +82,8 @@ typedef struct **/ msg_queue_t *msg_queue_new(size_t size); +bool msg_queue_initialize(msg_queue_t *queue, size_t size); + /** * msg_queue_push: * @queue : pointer to queue object @@ -131,6 +149,8 @@ void msg_queue_clear(msg_queue_t *queue); **/ void msg_queue_free(msg_queue_t *queue); +bool msg_queue_deinitialize(msg_queue_t *queue); + RETRO_END_DECLS #endif diff --git a/libretro-common/queues/message_queue.c b/libretro-common/queues/message_queue.c index 74db7f0bce..219d9d9e32 100644 --- a/libretro-common/queues/message_queue.c +++ b/libretro-common/queues/message_queue.c @@ -28,23 +28,21 @@ #include #include -struct queue_elem +static bool msg_queue_initialize_internal(msg_queue_t *queue, size_t size) { - char *msg; - char *title; - unsigned duration; - unsigned prio; - enum message_queue_icon icon; - enum message_queue_category category; -}; + struct queue_elem **elems = (struct queue_elem**)calloc(size + 1, + sizeof(struct queue_elem*)); -struct msg_queue -{ - char *tmp_msg; - struct queue_elem **elems; - size_t ptr; - size_t size; -}; + if (!elems) + return false; + + queue->tmp_msg = NULL; + queue->elems = elems; + queue->ptr = 1; + queue->size = size + 1; + + return true; +} /** * msg_queue_new: @@ -57,30 +55,27 @@ struct msg_queue **/ msg_queue_t *msg_queue_new(size_t size) { - struct queue_elem **elems = NULL; msg_queue_t *queue = (msg_queue_t*)malloc(sizeof(*queue)); if (!queue) return NULL; - queue->size = size + 1; - queue->tmp_msg = NULL; - - elems = (struct queue_elem**)calloc(queue->size, - sizeof(struct queue_elem*)); - - if (!elems) + if (!msg_queue_initialize_internal(queue, size)) { free(queue); return NULL; } - queue->elems = elems; - queue->ptr = 1; - 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 @@ -89,14 +84,26 @@ msg_queue_t *msg_queue_new(size_t size) **/ void msg_queue_free(msg_queue_t *queue) { - if (queue) - { - msg_queue_clear(queue); - free(queue->elems); - } + if (!queue) + return; + msg_queue_clear(queue); + free(queue->elems); free(queue); } +bool msg_queue_deinitialize(msg_queue_t *queue) +{ + if (!queue) + return false; + msg_queue_clear(queue); + free(queue->elems); + queue->elems = NULL; + queue->tmp_msg = NULL; + queue->ptr = 0; + queue->size = 0; + return true; +} + /** * msg_queue_push: * @queue : pointer to queue object diff --git a/retroarch.c b/retroarch.c index a9ef059546..93a5117dda 100644 --- a/retroarch.c +++ b/retroarch.c @@ -2004,7 +2004,6 @@ struct rarch_state const char **menu_input_dialog_keyboard_buffer; #endif core_option_manager_t *runloop_core_options; - msg_queue_t *runloop_msg_queue; const record_driver_t *recording_driver; void *recording_data; @@ -2124,6 +2123,7 @@ struct rarch_state struct retro_subsystem_rom_info subsystem_data_roms[SUBSYSTEM_MAX_SUBSYSTEMS] [SUBSYSTEM_MAX_SUBSYSTEM_ROMS]; /* ptr alignment */ + msg_queue_t runloop_msg_queue; /* ptr alignment */ gfx_ctx_driver_t current_video_context; /* ptr alignment */ content_state_t content_st; /* ptr alignment */ midi_event_t midi_drv_input_event; /* ptr alignment */ @@ -12572,10 +12572,7 @@ static void retroarch_msg_queue_deinit(struct rarch_state *p_rarch) { RUNLOOP_MSG_QUEUE_LOCK(); - if (!p_rarch->runloop_msg_queue) - return; - - msg_queue_free(p_rarch->runloop_msg_queue); + msg_queue_deinitialize(&p_rarch->runloop_msg_queue); RUNLOOP_MSG_QUEUE_UNLOCK(); #ifdef HAVE_THREADS @@ -12583,14 +12580,13 @@ static void retroarch_msg_queue_deinit(struct rarch_state *p_rarch) p_rarch->runloop_msg_queue_lock = NULL; #endif - p_rarch->runloop_msg_queue = NULL; p_rarch->runloop_msg_queue_size = 0; } static void retroarch_msg_queue_init(struct rarch_state *p_rarch) { retroarch_msg_queue_deinit(p_rarch); - p_rarch->runloop_msg_queue = msg_queue_new(8); + msg_queue_initialize(&p_rarch->runloop_msg_queue, 8); #ifdef HAVE_THREADS p_rarch->runloop_msg_queue_lock = slock_new(); @@ -33075,9 +33071,9 @@ static void video_driver_frame(const void *data, unsigned width, RUNLOOP_MSG_QUEUE_LOCK(); msg_found = msg_queue_extract( - p_rarch->runloop_msg_queue, &msg_entry); + &p_rarch->runloop_msg_queue, &msg_entry); p_rarch->runloop_msg_queue_size = msg_queue_size( - p_rarch->runloop_msg_queue); + &p_rarch->runloop_msg_queue); RUNLOOP_MSG_QUEUE_UNLOCK(); if (msg_found) @@ -33107,8 +33103,8 @@ static void video_driver_frame(const void *data, unsigned width, { const char *msg = NULL; RUNLOOP_MSG_QUEUE_LOCK(); - msg = msg_queue_pull(p_rarch->runloop_msg_queue); - p_rarch->runloop_msg_queue_size = msg_queue_size(p_rarch->runloop_msg_queue); + msg = msg_queue_pull(&p_rarch->runloop_msg_queue); + p_rarch->runloop_msg_queue_size = msg_queue_size(&p_rarch->runloop_msg_queue); if (msg) strlcpy(video_driver_msg, msg, sizeof(video_driver_msg)); RUNLOOP_MSG_QUEUE_UNLOCK(); @@ -38299,14 +38295,14 @@ void runloop_msg_queue_push(const char *msg, #endif { if (flush) - msg_queue_clear(p_rarch->runloop_msg_queue); + msg_queue_clear(&p_rarch->runloop_msg_queue); - if (p_rarch->runloop_msg_queue) - msg_queue_push(p_rarch->runloop_msg_queue, msg, - prio, duration, - title, icon, category); + msg_queue_push(&p_rarch->runloop_msg_queue, msg, + prio, duration, + title, icon, category); - p_rarch->runloop_msg_queue_size = msg_queue_size(p_rarch->runloop_msg_queue); + p_rarch->runloop_msg_queue_size = msg_queue_size( + &p_rarch->runloop_msg_queue); } ui_companion_driver_msg_queue_push(p_rarch,