A string list can be used as a stack, but should we? A later patch shows how useful this will be. In an earlier iteration of this patch it was suggested to return the last element or NULL (if empty), to enable a pattern of while ((item = string_list_pop(&list)) work_on(item); But it turns out that this pattern is error-prone as the memory model of the returned item is unclear. The caller would need to free the item->string if the string list is a duplicating list; and more importantly if there are further calls to insert items into the list, a memory reallocation might occur, invalidating the returned item at some time in the future. That is why the implementation doesn't return any item and the caller needs to be worded as while (list.nr) { work_on(last(&list])); string_list_pop(&list, free_util); } Also provide the function to access the last item in the list. Signed-off-by: Stefan Beller <sbeller@xxxxxxxxxx> --- string-list.c | 14 ++++++++++++++ string-list.h | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/string-list.c b/string-list.c index 1ebbe1f56ea..21559f222a7 100644 --- a/string-list.c +++ b/string-list.c @@ -80,6 +80,20 @@ void string_list_remove(struct string_list *list, const char *string, } } +void string_list_pop(struct string_list *list, int free_util) +{ + if (list->nr == 0) + BUG("tried to remove an item from empty string list"); + + if (list->strdup_strings) + free(list->items[list->nr - 1].string); + + if (free_util) + free(list->items[list->nr - 1].util); + + list->nr--; +} + int string_list_has_string(const struct string_list *list, const char *string) { int exact_match; diff --git a/string-list.h b/string-list.h index 5b22560cf19..d7cdf38e57a 100644 --- a/string-list.h +++ b/string-list.h @@ -191,6 +191,17 @@ extern void string_list_remove(struct string_list *list, const char *string, */ struct string_list_item *string_list_lookup(struct string_list *list, const char *string); +/** + * Removes the last item from the list. + * The caller must ensure that the list is not empty. + */ +void string_list_pop(struct string_list *list, int free_util); + +inline struct string_list_item *string_list_last(struct string_list *list) +{ + return &list->items[list->nr - 1]; +} + /* * Remove all but the first of consecutive entries with the same * string value. If free_util is true, call free() on the util -- 2.19.0.rc1.350.ge57e33dbd1-goog