mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-01-06 08:30:15 +01:00
glib-aux: clear iterator in nm_dedup_multi_iter_{next,prev}() at the end
It seems slightly nicer not to leave a dangling pointer at the
end of the iteration. Then you could do something like
nm_dedup_multi_iter_init(&iter, head_entry);
while (nm_dedup_multi_iter_next(&iter)) {
if (some_condition())
break;
}
if (!iter.current)
printf("iterated to the end\n");
As nm_dedup_multi_iter_next() and nm_dedup_multi_iter_init() are inline
functions, the compiler should even be able to see that the initial
setting becomes unnecessary (the field will be initialized by the
first nm_dedup_multi_iter_next()). Likewise, the final clearing
of the field might also be optimized away at the end of the iteration
(if, as in the common case, the iterator is not accessed afterwards).
(cherry picked from commit 53070705b0)
This commit is contained in:
parent
1803370cb2
commit
e60c52829c
1 changed files with 6 additions and 2 deletions
|
|
@ -352,8 +352,10 @@ nm_dedup_multi_iter_next(NMDedupMultiIter *iter)
|
|||
{
|
||||
g_return_val_if_fail(iter, FALSE);
|
||||
|
||||
if (!iter->_next)
|
||||
if (!iter->_next) {
|
||||
iter->current = NULL;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* we always look ahead for the next. This way, the user
|
||||
* may delete the current entry (but no other entries). */
|
||||
|
|
@ -370,8 +372,10 @@ nm_dedup_multi_iter_prev(NMDedupMultiIter *iter)
|
|||
{
|
||||
g_return_val_if_fail(iter, FALSE);
|
||||
|
||||
if (!iter->_prev)
|
||||
if (!iter->_prev) {
|
||||
iter->current = NULL;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* we always look ahead for the prev. This way, the user
|
||||
* may delete the current entry (but no other entries). */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue