Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=0597259bb0bd8ce921b5a38830b2d49d67ac59e7
commit 0597259bb0bd8ce921b5a38830b2d49d67ac59e7 Author: Michel Hermier <[email protected]> Date: Tue May 21 08:15:12 2013 +0200 libflib: Add private f_list_foreach_safe, not *safe* with current list implementation. diff --git a/lib/libflib/flist.c b/lib/libflib/flist.c index 1ace41f..600ee48 100644 --- a/lib/libflib/flist.c +++ b/lib/libflib/flist.c @@ -29,6 +29,12 @@ #include "flistaccumulator.h" #include "fstdlib.h" +/* DO NOT MAKE PUBLIC FOR NOW: + * Require list implemantation change. + */ +static +void f_list_foreach_safe (FList *list, FVisitorFunc fn, void *user_data); + static void *f_ptrcpy(const void *p) { return (void *)p; @@ -43,19 +49,27 @@ FListItem *f_listitem_new (void *data) { return item; } -/** - * Remove the item from it's list and free it. - */ -void f_listitem_delete (FList *item, FVisitorFunc fn, void *user_data) { +static +void _f_listitem_delete (FListItem *item, FVisitor *visitor) { if (item != NULL) { - if (fn != NULL) { - fn (item->data, user_data); - } + f_visit (item->data, visitor); f_listitem_remove (item); - free (item); + f_free (item); } } +/** + * Remove the item from it's list and free it. + */ +void f_listitem_delete (FListItem *item, FVisitorFunc fn, void *user_data) { + FVisitor visitor = { + .fn = fn, + .user_data = user_data + }; + + _f_listitem_delete (item, &visitor); +} + void *f_listitem_get (FListItem *item) { return item != NULL ? item->data : NULL; } @@ -95,12 +109,12 @@ FList *f_list_new () { } void f_list_delete (FList *list, FVisitorFunc fn, void *user_data) { - FList *next = list; + FVisitor visitor = { + .fn = fn, + .user_data = user_data + }; - while ((list = next) != NULL) { - next = list->next; - f_listitem_delete (list, fn, user_data); - } + f_list_foreach_safe (list, (FVisitorFunc)_f_listitem_delete, &visitor); } /** @@ -313,7 +327,7 @@ FListItem *f_list_find_custom (FList *list, const void *data, FCompareFunc cfn, .user_data = user_data }; - return f_list_detect (list, f_comparedetect, &comparedetector); + return f_list_detect (list, (FDetectFunc)f_comparedetect, &comparedetector); } void f_list_foreach (FList *list, FVisitorFunc fn, void *user_data) { @@ -324,6 +338,20 @@ void f_list_foreach (FList *list, FVisitorFunc fn, void *user_data) { } } +/** + * A foreach safe in the sence you can detach the current element. + * + * Differs from foreach since it pass an FListItem instead of data. + */ +void f_list_foreach_safe (FList *list, FVisitorFunc fn, void *user_data) { + FListItem *it, *next = f_list_begin (list), *end = f_list_end (list); + + for (it = next; it != end; it = next) { + next = it->next; + fn (it, user_data); + } +} + /* Reverse the order of a list * * The caller is responsible for freeing the old list _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
