difference betw. "_SAFE" and non-"_SAFE" versions of SYS_SLIST_FOR_EACH_CONTAINER #api


yshragai.firmware@...
 

Hi,
I just want to make sure that I understand the difference between SYS_SLIST_FOR_EACH_CONTAINER and SYS_SLIST_FOR_EACH_CONTAINER_SAFE.
 
The documentation says for the former:
Note: the loop is unsafe and thus __cn should not be detached.
And for the latter:
Note: __cn can be detached, it will not break the loop.
I want to make sure I understand what "safety" it's referring to and what "can be detached" means.
 
I see in the code that, for the non-"_SAFE" version, the increment expression relies on the value of __cn in the iteration:
__cn = Z_GENLIST_PEEK_NEXT_CONTAINER(__lname, __cn, __n)

Whereas in the "_SAFE" version, it precalculates the next pointer, and uses this to increment:
__cn != NULL; __cn = __cns,
__cns = Z_GENLIST_PEEK_NEXT_CONTAINER(__lname, __cn, __n)
 
So I'm guessing that this "detachment" refers to removal from the list? In the non-"_SAFE" version, if the body of the loop removes __cn from the list and frees it, then the increment expression would clearly fail. Is this the "safety" that's being referred to?
 
Thanks!