9
0
Fork 0

stringlist: use seperately allocated string

Allocate the string in string list seperately instead of
embedding a zero length string into struct stringlist.
Besides looking cleaner this allows us to implement a
string_list_asprintf.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
This commit is contained in:
Sascha Hauer 2012-04-19 10:34:11 +08:00 committed by Jean-Christophe PLAGNIOL-VILLARD
parent 6b489256ee
commit 34faa2e7ca
2 changed files with 8 additions and 8 deletions

View File

@ -5,7 +5,7 @@
struct string_list {
struct list_head list;
char str[0];
char *str;
};
int string_list_add(struct string_list *sl, char *str);
@ -22,8 +22,10 @@ static inline void string_list_free(struct string_list *sl)
{
struct string_list *entry, *safe;
list_for_each_entry_safe(entry, safe, &sl->list, list)
list_for_each_entry_safe(entry, safe, &sl->list, list) {
free(entry->str);
free(entry);
}
}
#endif /* __STRING_H */

View File

@ -16,9 +16,8 @@ int string_list_add(struct string_list *sl, char *str)
{
struct string_list *new;
new = xmalloc(sizeof(struct string_list) + strlen(str) + 1);
strcpy(new->str, str);
new = xmalloc(sizeof(*new));
new->str = xstrdup(str);
list_add_tail(&new->list, &sl->list);
@ -29,9 +28,8 @@ int string_list_add_sorted(struct string_list *sl, char *str)
{
struct string_list *new;
new = xmalloc(sizeof(struct string_list) + strlen(str) + 1);
strcpy(new->str, str);
new = xmalloc(sizeof(*new));
new->str = xstrdup(str);
list_add_sort(&new->list, &sl->list, string_list_compare);