From e1b11593fc262b6e8fd886245163d43664845a75 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sat, 27 Apr 2019 07:32:40 +0200 Subject: [PATCH] (file_list.c) Cleanups/optimizations --- libretro-common/include/lists/file_list.h | 2 - libretro-common/lists/file_list.c | 143 +++++++--------------- menu/drivers/ozone/ozone.c | 1 - menu/drivers/stripes.c | 1 - menu/drivers/xmb.c | 1 - 5 files changed, 43 insertions(+), 105 deletions(-) diff --git a/libretro-common/include/lists/file_list.h b/libretro-common/include/lists/file_list.h index 1c18bf226b..defbc9ab72 100644 --- a/libretro-common/include/lists/file_list.h +++ b/libretro-common/include/lists/file_list.h @@ -101,8 +101,6 @@ void file_list_pop(file_list_t *list, size_t *directory_ptr); void file_list_clear(file_list_t *list); -void file_list_copy(const file_list_t *src, file_list_t *dst); - void file_list_get_last(const file_list_t *list, const char **path, const char **label, unsigned *type, size_t *entry_idx); diff --git a/libretro-common/lists/file_list.c b/libretro-common/lists/file_list.c index 8480d4e79c..7abe5bd2b9 100644 --- a/libretro-common/lists/file_list.c +++ b/libretro-common/lists/file_list.c @@ -50,28 +50,6 @@ bool file_list_reserve(file_list_t *list, size_t nitems) return new_data != NULL; } -static void file_list_add(file_list_t *list, unsigned idx, - const char *path, const char *label, - unsigned type, size_t directory_ptr, - size_t entry_idx) -{ - list->list[idx].path = NULL; - list->list[idx].label = NULL; - list->list[idx].alt = NULL; - list->list[idx].type = type; - list->list[idx].directory_ptr = directory_ptr; - list->list[idx].entry_idx = entry_idx; - list->list[idx].userdata = NULL; - list->list[idx].actiondata = NULL; - - if (label) - list->list[idx].label = strdup(label); - if (path) - list->list[idx].path = strdup(path); - - list->size++; -} - bool file_list_prepend(file_list_t *list, const char *path, const char *label, unsigned type, size_t directory_ptr, @@ -110,8 +88,21 @@ bool file_list_insert(file_list_t *list, free(copy); } - file_list_add(list, (unsigned)idx, path, label, type, - directory_ptr, entry_idx); + list->list[idx].path = NULL; + list->list[idx].label = NULL; + list->list[idx].alt = NULL; + list->list[idx].type = type; + list->list[idx].directory_ptr = directory_ptr; + list->list[idx].entry_idx = entry_idx; + list->list[idx].userdata = NULL; + list->list[idx].actiondata = NULL; + + if (label) + list->list[idx].label = strdup(label); + if (path) + list->list[idx].path = strdup(path); + + list->size++; return true; } @@ -121,13 +112,27 @@ bool file_list_append(file_list_t *list, unsigned type, size_t directory_ptr, size_t entry_idx) { + unsigned idx = (unsigned)list->size; /* Expand file list if needed */ - if (list->size >= list->capacity) + if (idx >= list->capacity) if (!file_list_reserve(list, list->capacity * 2 + 1)) return false; - file_list_add(list, (unsigned)list->size, path, label, type, - directory_ptr, entry_idx); + list->list[idx].path = NULL; + list->list[idx].label = NULL; + list->list[idx].alt = NULL; + list->list[idx].type = type; + list->list[idx].directory_ptr = directory_ptr; + list->list[idx].entry_idx = entry_idx; + list->list[idx].userdata = NULL; + list->list[idx].actiondata = NULL; + + if (label) + list->list[idx].label = strdup(label); + if (path) + list->list[idx].path = strdup(path); + + list->size++; return true; } @@ -221,61 +226,6 @@ void file_list_clear(file_list_t *list) list->size = 0; } -void file_list_copy(const file_list_t *src, file_list_t *dst) -{ - struct item_file *item = NULL; - - if (!src || !dst) - return; - - if (dst->list) - { - for (item = dst->list; item < &dst->list[dst->size]; ++item) - { - if (!item) - continue; - - if (item->path) - free(item->path); - item->path = NULL; - - if (item->label) - free(item->label); - item->label = NULL; - - if (item->alt) - free(item->alt); - item->alt = NULL; - } - - free(dst->list); - dst->list = NULL; - } - - dst->size = 0; - dst->capacity = 0; - dst->list = (struct item_file*)malloc(src->size * sizeof(struct item_file)); - - if (!dst->list) - return; - - dst->size = dst->capacity = src->size; - - memcpy(dst->list, src->list, dst->size * sizeof(struct item_file)); - - for (item = dst->list; item < &dst->list[dst->size]; ++item) - { - if (item->path) - item->path = strdup(item->path); - - if (item->label) - item->label = strdup(item->label); - - if (item->alt) - item->alt = strdup(item->alt); - } -} - void file_list_set_label_at_offset(file_list_t *list, size_t idx, const char *label) { @@ -318,12 +268,10 @@ void file_list_set_alt_at_offset(file_list_t *list, size_t idx, void file_list_get_alt_at_offset(const file_list_t *list, size_t idx, const char **alt) { - if (!list) - return; - - if (alt) - *alt = list->list[idx].alt ? - list->list[idx].alt : list->list[idx].path; + if (list && alt) + *alt = list->list[idx].alt + ? list->list[idx].alt + : list->list[idx].path; } static int file_list_alt_cmp(const void *a_, const void *b_) @@ -366,16 +314,14 @@ void *file_list_get_userdata_at_offset(const file_list_t *list, size_t idx) void file_list_set_userdata(const file_list_t *list, size_t idx, void *ptr) { - if (!list || !ptr) - return; - list->list[idx].userdata = ptr; + if (list && ptr) + list->list[idx].userdata = ptr; } void file_list_set_actiondata(const file_list_t *list, size_t idx, void *ptr) { - if (!list || !ptr) - return; - list->list[idx].actiondata = ptr; + if (list && ptr) + list->list[idx].actiondata = ptr; } void *file_list_get_actiondata_at_offset(const file_list_t *list, size_t idx) @@ -431,10 +377,7 @@ void file_list_get_last(const file_list_t *list, const char **path, const char **label, unsigned *file_type, size_t *entry_idx) { - if (!list) - return; - - if (list->size) + if (list && list->size) file_list_get_at_offset(list, list->size - 1, path, label, file_type, entry_idx); } @@ -464,7 +407,7 @@ bool file_list_search(const file_list_t *list, const char *needle, size_t *idx) { /* Found match with first chars, best possible match. */ *idx = i; - ret = true; + ret = true; break; } else if (str && !ret) @@ -472,7 +415,7 @@ bool file_list_search(const file_list_t *list, const char *needle, size_t *idx) /* Found mid-string match, but try to find a match with * first characters before we settle. */ *idx = i; - ret = true; + ret = true; } } diff --git a/menu/drivers/ozone/ozone.c b/menu/drivers/ozone/ozone.c index 58175ab5b4..3c00fbd932 100644 --- a/menu/drivers/ozone/ozone.c +++ b/menu/drivers/ozone/ozone.c @@ -1971,7 +1971,6 @@ static void ozone_list_deep_copy(const file_list_t *src, file_list_t *dst, menu_animation_kill_by_tag(&tag); - /* use true here because file_list_copy() doesn't free actiondata */ ozone_free_list_nodes(dst, true); file_list_clear(dst); diff --git a/menu/drivers/stripes.c b/menu/drivers/stripes.c index 9515008558..8caf696d46 100644 --- a/menu/drivers/stripes.c +++ b/menu/drivers/stripes.c @@ -3831,7 +3831,6 @@ static void stripes_list_deep_copy(const file_list_t *src, file_list_t *dst, menu_animation_kill_by_tag(&tag); - /* use true here because file_list_copy() doesn't free actiondata */ stripes_free_list_nodes(dst, true); file_list_clear(dst); diff --git a/menu/drivers/xmb.c b/menu/drivers/xmb.c index 00b802fd69..15d4b94afe 100644 --- a/menu/drivers/xmb.c +++ b/menu/drivers/xmb.c @@ -5067,7 +5067,6 @@ static void xmb_list_deep_copy(const file_list_t *src, file_list_t *dst, menu_animation_kill_by_tag(&tag); - /* use true here because file_list_copy() doesn't free actiondata */ xmb_free_list_nodes(dst, true); file_list_clear(dst);