(file_list.c) Add file_list_reserve()
This commit is contained in:
parent
6c7a354822
commit
d40deb737d
|
@ -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);
|
||||
|
|
|
@ -29,28 +29,25 @@
|
|||
#include <string/stdstring.h>
|
||||
#include <compat/strcasestr.h>
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue