From d40deb737d3bd7448854e9f965865db6d7f858b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Higor=20Eur=C3=ADpedes?= Date: Sun, 3 Sep 2017 14:58:01 -0300 Subject: [PATCH] (file_list.c) Add file_list_reserve() --- libretro-common/include/lists/file_list.h | 12 +++++++ libretro-common/lists/file_list.c | 43 +++++++++-------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/libretro-common/include/lists/file_list.h b/libretro-common/include/lists/file_list.h index 67d3875fc9..85ea8dbc3a 100644 --- a/libretro-common/include/lists/file_list.h +++ b/libretro-common/include/lists/file_list.h @@ -71,6 +71,18 @@ void *file_list_get_actiondata_at_offset(const file_list_t *list, */ void file_list_free(file_list_t *list); +/** + * @brief makes the list big enough to contain at least nitems + * + * This function will not change the capacity if nitems is smaller + * than the current capacity. + * + * @param list + * @param nitems + * @return whether or not the operation succeeded + */ +bool file_list_reserve(file_list_t *list, size_t nitems); + bool file_list_append(file_list_t *userdata, const char *path, const char *label, unsigned type, size_t current_directory_ptr, size_t entry_index); diff --git a/libretro-common/lists/file_list.c b/libretro-common/lists/file_list.c index 9ad11a8805..87e23c6d74 100644 --- a/libretro-common/lists/file_list.c +++ b/libretro-common/lists/file_list.c @@ -29,28 +29,25 @@ #include #include -/** - * file_list_capacity: - * @list : pointer to file list - * @cap : new capacity for file list. - * - * Change maximum capacity of file list's size. - * - * Returns: true (1) if successful, otherwise false (0). - **/ -static struct item_file *realloc_file_list_capacity(file_list_t *list, size_t cap) +bool file_list_reserve(file_list_t *list, size_t nitems) { - struct item_file *new_data = (struct item_file*)realloc(list->list, - cap * sizeof(struct item_file)); + const size_t item_size = sizeof(struct item_file); + struct item_file *new_data; - if (!new_data) - return NULL; + if (nitems < list->capacity || nitems > (size_t)-1/item_size) + return false; - if (cap > list->capacity) - memset(&new_data[list->capacity], 0, - sizeof(*new_data) * (cap - list->capacity)); + new_data = (struct item_file*)realloc(list->list, nitems * item_size); - return new_data; + if (new_data) + { + memset(&new_data[list->capacity], 0, item_size * (nitems - list->capacity)); + + list->list = new_data; + list->capacity = nitems; + } + + return new_data != NULL; } static void file_list_add(file_list_t *list, unsigned idx, @@ -78,16 +75,8 @@ static void file_list_add(file_list_t *list, unsigned idx, static bool file_list_expand_if_needed(file_list_t *list) { if (list->size >= list->capacity) - { - size_t new_capacity = list->capacity * 2 + 1; - struct item_file *items = realloc_file_list_capacity( - list, new_capacity); + return file_list_reserve(list, list->capacity * 2 + 1); - if (!items) - return false; - list->list = items; - list->capacity = new_capacity; - } return true; }