In the comments of linux/list.h
it is written that:
list_del_entry
: Note: list_empty
on entry does not return true after this, the entry is in an undefined state. list_del
: This is only for internal list manipulation where we know the prev/next entries already!
So, how would I safely remove an object from linked list and make sure that list_empty
is functional or make sure that next linked list node deletion is correct?
This is my implementation currently:
struct kool_list{
int to;
struct list_head list;
int from;
};
struct kool_list *tmp;
struct list_head *pos, *q;
struct kool_list mylist;
list_for_each_safe(pos, q, &mylist.list){
tmp= list_entry(pos, struct kool_list, list);
printf("freeing item to= %d from= %d\n", tmp->to, tmp->from);
list_del(pos);
free(tmp);
}
I think you misunderstand the comments. The first one says that list_empty(&entry->list)
will not return true. However, if you remove all elements from the list (the way you do it is correct) and do list_empty(&mylist.list)
you will get true as a result.
If for some reason you want to keep the entry's struct list_head
in an internally consistent state, use list_del_init
.
Secondly, __list_del
is for internal usage only, list_del
is fair game.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With