Fix ao2_iterator API to hold references to containers being iterated.

See Mantis issue for details of what prompted this change.

Additional notes:

This patch changes the ao2_iterator API in two ways: F_AO2I_DONTLOCK
has become an enum instead of a macro, with a name that fits our
naming policy; also, it is now necessary to call
ao2_iterator_destroy() on any iterator that has been
created. Currently this only releases the reference to the container
being iterated, but in the future this could also release other
resources used by the iterator, if the iterator implementation changes
to use additional resources.

(closes issue #15987)
Reported by: kpfleming

Review: https://reviewboard.asterisk.org/r/383/


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@222152 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Kevin P. Fleming
2009-10-06 01:16:36 +00:00
parent dfb8d75f23
commit 2ad7cb7e87
5 changed files with 107 additions and 7 deletions

View File

@@ -580,10 +580,21 @@ struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags)
.c = c,
.flags = flags
};
ao2_ref(c, +1);
return a;
}
/*!
* destroy an iterator
*/
void ao2_iterator_destroy(struct ao2_iterator *i)
{
ao2_ref(i->c, -1);
i->c = NULL;
}
/*
* move to the next element in the container.
*/
@@ -596,7 +607,7 @@ void * ao2_iterator_next(struct ao2_iterator *a)
if (INTERNAL_OBJ(a->c) == NULL)
return NULL;
if (!(a->flags & F_AO2I_DONTLOCK))
if (!(a->flags & AO2_ITERATOR_DONTLOCK))
ao2_lock(a->c);
/* optimization. If the container is unchanged and
@@ -637,7 +648,7 @@ found:
ao2_ref(ret, 1);
}
if (!(a->flags & F_AO2I_DONTLOCK))
if (!(a->flags & AO2_ITERATOR_DONTLOCK))
ao2_unlock(a->c);
return ret;