Actions
Bug #14591
openlist_create(9f): provide example of safely deleting specific entries in the loop
Status:
New
Priority:
Low
Assignee:
-
Category:
manpage - manual pages
Start date:
Due date:
% Done:
0%
Estimated time:
Difficulty:
Bite-size
Tags:
Gerrit CR:
Description
This puzzled me a bit, so I have taken the following logic from STAILQ_FOREACH_SAFE
to safely remove specific entries while walking the list, which could be documented in the man page:
list_t list; foo_t foo, tfoo; ... for (foo = list_head(&list); foo != NULL && (tfoo = list_next(&list, foo), 1); foo = tfoo) { ... if (condition) list_remove(&list, foo); }
It is not ideal, of course, so if there is any better solution, that could be added instead.
Updated by Yuri Pankov 5 months ago
- Subject changed from list_create(9f): provide example of safely deleting entries in the loop to list_create(9f): provide example of safely deleting specific entries in the loop
- Description updated (diff)
Updated by Andy Fiddaman 5 months ago
There's an example in smrt which is a bit easier to read (same approach though, just moving the next assignment into the loop). I like the next
variable name, and the fact that it would be safe to continue;
in the loop (same as your example)
list_t list;
item_t item, next;
for (item = list_head(&list), next = NULL; item != NULL; item = next) {
next = list_next(&list, item);
if (condition) {
list_remove(&list, item);
free_item(item);
}
}
Updated by Joshua M. Clulow 5 months ago
I definitely find Andy's example easier to read -- though I may be biased as I probably wrote that code in smrt.
Actions