[asterisk-commits] kpfleming: branch 1.6.1 r222186 - in /branches/1.6.1: ./ apps/ channels/ func...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Mon Oct 5 20:36:42 CDT 2009
Author: kpfleming
Date: Mon Oct 5 20:36:36 2009
New Revision: 222186
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=222186
Log:
Merged revisions 222176 via svnmerge from
https://origsvn.digium.com/svn/asterisk/trunk
................
r222176 | kpfleming | 2009-10-05 20:24:24 -0500 (Mon, 05 Oct 2009) | 27 lines
Recorded merge of revisions 222152 via svnmerge from
https://origsvn.digium.com/svn/asterisk/branches/1.4
........
r222152 | kpfleming | 2009-10-05 20:16:36 -0500 (Mon, 05 Oct 2009) | 20 lines
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/
........
................
Modified:
branches/1.6.1/ (props changed)
branches/1.6.1/apps/app_queue.c
branches/1.6.1/channels/chan_console.c
branches/1.6.1/channels/chan_iax2.c
branches/1.6.1/channels/chan_sip.c
branches/1.6.1/funcs/func_dialgroup.c
branches/1.6.1/include/asterisk/astobj2.h
branches/1.6.1/main/astobj2.c
branches/1.6.1/res/res_musiconhold.c
branches/1.6.1/res/res_odbc.c
branches/1.6.1/res/res_phoneprov.c
Propchange: branches/1.6.1/
------------------------------------------------------------------------------
Binary property 'trunk-merged' - no diff available.
Modified: branches/1.6.1/apps/app_queue.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/apps/app_queue.c?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/apps/app_queue.c (original)
+++ branches/1.6.1/apps/app_queue.c Mon Oct 5 20:36:36 2009
@@ -690,11 +690,13 @@
} else {
ao2_unlock(q);
ao2_ref(member, -1);
+ ao2_iterator_destroy(&mem_iter);
return QUEUE_NORMAL;
}
break;
}
}
+ ao2_iterator_destroy(&mem_iter);
ao2_unlock(q);
return result;
@@ -777,7 +779,6 @@
if ((slash_pos = strchr(interface, '/')))
if ((slash_pos = strchr(slash_pos + 1, '/')))
*slash_pos = '\0';
-
if (!strcasecmp(interface, sc->dev))
break;
}
@@ -993,7 +994,7 @@
int ret = 0;
ast_copy_string(tmpmem.interface, interface, sizeof(tmpmem.interface));
- queue_iter = ao2_iterator_init(queues, lock_queue_container ? 0 : F_AO2I_DONTLOCK);
+ queue_iter = ao2_iterator_init(queues, lock_queue_container ? 0 : AO2_ITERATOR_DONTLOCK);
while ((q = ao2_iterator_next(&queue_iter))) {
ao2_lock(q);
mem_iter = ao2_iterator_init(q->members, 0);
@@ -1004,9 +1005,11 @@
break;
}
}
+ ao2_iterator_destroy(&mem_iter);
ao2_unlock(q);
queue_unref(q);
}
+ ao2_iterator_destroy(&queue_iter);
return ret;
}
@@ -1384,6 +1387,7 @@
}
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
/* Create a new member */
if (!found) {
@@ -1416,6 +1420,7 @@
}
ao2_ref(cur, -1);
}
+ ao2_iterator_destroy(&mem_iter);
}
/*! \brief Free queue's member list then its string fields */
@@ -1563,6 +1568,7 @@
m->dead = 1;
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
while ((interface = ast_category_browse(member_config, interface))) {
rt_handle_member_record(q, interface,
@@ -1584,6 +1590,7 @@
}
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
ao2_unlock(q);
@@ -1672,6 +1679,7 @@
m->dead = 1;
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
while ((interface = ast_category_browse(member_config, interface))) {
rt_handle_member_record(q, interface,
@@ -1693,6 +1701,7 @@
}
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
ao2_unlock(q);
ao2_unlock(queues);
ast_config_destroy(member_config);
@@ -2115,6 +2124,7 @@
break;
}
}
+ ao2_iterator_destroy(&mem_iter);
return avl;
}
@@ -2153,6 +2163,7 @@
break;
}
}
+ ao2_iterator_destroy(&queue_iter);
return found;
}
@@ -3064,6 +3075,7 @@
ao2_unlock(qtmp);
ao2_ref(qtmp, -1);
}
+ ao2_iterator_destroy(&queue_iter);
} else {
ao2_lock(q);
time(&member->lastcall);
@@ -3468,6 +3480,7 @@
if (!tmp) {
ao2_ref(cur, -1);
ao2_unlock(qe->parent);
+ ao2_iterator_destroy(&memi);
if (use_weight)
ao2_unlock(queues);
goto out;
@@ -3476,6 +3489,7 @@
if (!(datastore = ast_datastore_alloc(&dialed_interface_info, NULL))) {
ao2_ref(cur, -1);
ao2_unlock(qe->parent);
+ ao2_iterator_destroy(&memi);
if (use_weight)
ao2_unlock(queues);
free(tmp);
@@ -3485,6 +3499,7 @@
if (!(dialed_interfaces = ast_calloc(1, sizeof(*dialed_interfaces)))) {
ao2_ref(cur, -1);
ao2_unlock(&qe->parent);
+ ao2_iterator_destroy(&memi);
if (use_weight)
ao2_unlock(queues);
free(tmp);
@@ -3522,6 +3537,7 @@
if (!(di = ast_calloc(1, sizeof(*di) + strlen(cur->interface)))) {
ao2_ref(cur, -1);
ao2_unlock(qe->parent);
+ ao2_iterator_destroy(&memi);
if (use_weight)
ao2_unlock(queues);
free(tmp);
@@ -3556,6 +3572,7 @@
ast_free(tmp);
}
}
+ ao2_iterator_destroy(&memi);
if (qe->parent->timeoutpriority == TIMEOUT_PRIORITY_APP) {
/* Application arguments have higher timeout priority (behaviour for <=1.6) */
@@ -4095,10 +4112,13 @@
mem_iter = ao2_iterator_init(q->members, 0);
while ((mem = ao2_iterator_next(&mem_iter))) {
- if (!strcasecmp(interface, mem->interface))
+ if (!strcasecmp(interface, mem->interface)) {
+ ao2_iterator_destroy(&mem_iter);
return mem;
+ }
ao2_ref(mem, -1);
}
+ ao2_iterator_destroy(&mem_iter);
return NULL;
}
@@ -4139,6 +4159,7 @@
}
value_len += res;
}
+ ao2_iterator_destroy(&mem_iter);
if (value_len && !cur_member) {
if (ast_db_put(pm_family, pm_queue->name, value))
@@ -4332,6 +4353,7 @@
ao2_unlock(q);
queue_unref(q);
}
+ ao2_iterator_destroy(&queue_iter);
return found ? RESULT_SUCCESS : RESULT_FAILURE;
}
@@ -4370,6 +4392,7 @@
ao2_unlock(q);
queue_unref(q);
}
+ ao2_iterator_destroy(&queue_iter);
if (foundinterface) {
return RESULT_SUCCESS;
@@ -5166,6 +5189,7 @@
}
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
} else if (!strcasecmp(option, "free")) {
mem_iter = ao2_iterator_init(q->members, 0);
while ((m = ao2_iterator_next(&mem_iter))) {
@@ -5175,6 +5199,7 @@
}
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
} else /* must be "count" */
count = q->membercount;
ao2_unlock(q);
@@ -5220,6 +5245,7 @@
}
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
ao2_unlock(q);
queue_unref(q);
} else
@@ -5303,6 +5329,7 @@
}
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
ao2_unlock(q);
queue_unref(q);
} else
@@ -5532,7 +5559,7 @@
ao2_lock(queues);
use_weight=0;
/* Mark all queues as dead for the moment */
- queue_iter = ao2_iterator_init(queues, F_AO2I_DONTLOCK);
+ queue_iter = ao2_iterator_init(queues, AO2_ITERATOR_DONTLOCK);
while ((q = ao2_iterator_next(&queue_iter))) {
if (!q->realtime) {
q->dead = 1;
@@ -5769,7 +5796,7 @@
}
}
- queue_iter = ao2_iterator_init(queues, F_AO2I_DONTLOCK);
+ queue_iter = ao2_iterator_init(queues, AO2_ITERATOR_DONTLOCK);
ao2_lock(queues);
while ((q = ao2_iterator_next(&queue_iter))) {
float sl;
@@ -5833,6 +5860,7 @@
do_print(s, fd, out->str);
ao2_ref(mem, -1);
}
+ ao2_iterator_destroy(&mem_iter);
}
if (!q->head)
do_print(s, fd, " No Callers");
@@ -5852,6 +5880,7 @@
ao2_unlock(q);
queue_unref(q); /* Unref the iterator's reference */
}
+ ao2_iterator_destroy(&queue_iter);
ao2_unlock(queues);
if (!found) {
if (argc == 3)
@@ -5880,6 +5909,7 @@
}
queue_unref(q);
}
+ ao2_iterator_destroy(&queue_iter);
return ret;
}
@@ -5988,6 +6018,7 @@
}
ao2_ref(mem, -1);
}
+ ao2_iterator_destroy(&mem_iter);
for (qe = q->head; qe; qe = qe->next) {
if ((now - qe->start) > qlongestholdtime) {
qlongestholdtime = now - qe->start;
@@ -6008,6 +6039,7 @@
ao2_unlock(q);
queue_unref(q);
}
+ ao2_iterator_destroy(&queue_iter);
astman_append(s,
"Event: QueueSummaryComplete\r\n"
"%s"
@@ -6080,6 +6112,7 @@
}
ao2_ref(mem, -1);
}
+ ao2_iterator_destroy(&mem_iter);
/* List Queue Entries */
pos = 1;
for (qe = q->head; qe; qe = qe->next) {
@@ -6101,6 +6134,7 @@
ao2_unlock(q);
queue_unref(q);
}
+ ao2_iterator_destroy(&queue_iter);
astman_append(s,
"Event: QueueStatusComplete\r\n"
@@ -6400,13 +6434,17 @@
tmp = ast_strdup(m->interface);
ao2_ref(m, -1);
queue_unref(q);
+ ao2_iterator_destroy(&mem_iter);
+ ao2_iterator_destroy(&queue_iter);
return tmp;
}
ao2_ref(m, -1);
}
+ ao2_iterator_destroy(&mem_iter);
ao2_unlock(q);
queue_unref(q);
}
+ ao2_iterator_destroy(&queue_iter);
return NULL;
}
@@ -6723,6 +6761,7 @@
ao2_unlink(queues, q);
queue_unref(q);
}
+ ao2_iterator_destroy(&q_iter);
ao2_ref(queues, -1);
devicestate_tps = ast_taskprocessor_unreference(devicestate_tps);
ast_unload_realtime("queue_members");
Modified: branches/1.6.1/channels/chan_console.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/channels/chan_console.c?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/channels/chan_console.c (original)
+++ branches/1.6.1/channels/chan_console.c Mon Oct 5 20:36:36 2009
@@ -1020,6 +1020,7 @@
console_pvt_unlock(pvt);
unref_pvt(pvt);
}
+ ao2_iterator_destroy(&i);
ast_cli(a->fd, "=============================================================\n\n");
@@ -1171,9 +1172,12 @@
if (++x > a->n && !strncasecmp(pvt->name, a->word, strlen(a->word)))
res = ast_strdup(pvt->name);
unref_pvt(pvt);
- if (res)
+ if (res) {
+ ao2_iterator_destroy(&i);
return res;
+ }
}
+ ao2_iterator_destroy(&i);
}
return NULL;
}
@@ -1376,6 +1380,7 @@
}
unref_pvt(pvt);
}
+ ao2_iterator_destroy(&i);
}
/*!
@@ -1446,6 +1451,7 @@
stop_stream(pvt);
unref_pvt(pvt);
}
+ ao2_iterator_destroy(&i);
}
static int unload_module(void)
Modified: branches/1.6.1/channels/chan_iax2.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/channels/chan_iax2.c?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/channels/chan_iax2.c (original)
+++ branches/1.6.1/channels/chan_iax2.c Mon Oct 5 20:36:36 2009
@@ -1500,6 +1500,7 @@
}
peer_unref(peer);
}
+ ao2_iterator_destroy(&i);
if (!peer) {
peer = realtime_peer(NULL, &sin);
@@ -2246,6 +2247,7 @@
}
ao2_ref(peercnt, -1);
}
+ ao2_iterator_destroy(&i);
if (a->argc == 4) {
ast_cli(a->fd, "\nNon-CallToken Validation Limit: %d\nNon-CallToken Validated: %d\n", global_maxcallno_nonval, total_nonval_callno_used);
@@ -3495,6 +3497,7 @@
}
peer_unref(peer);
}
+ ao2_iterator_destroy(&i);
return res;
}
@@ -5234,6 +5237,7 @@
}
peer_unref(peer);
}
+ ao2_iterator_destroy(&i);
return res;
}
@@ -6138,6 +6142,7 @@
user->contexts ? user->contexts->context : DEFAULT_CONTEXT,
user->ha ? "Yes" : "No", pstr);
}
+ ao2_iterator_destroy(&i);
if (havepattern)
regfree(®exbuf);
@@ -6263,6 +6268,7 @@
peer->encmethods ? "(E)" : " ", status, term);
total_peers++;
}
+ ao2_iterator_destroy(&i);
if (!s)
ast_cli(fd,"%d iax2 peers [%d online, %d offline, %d unmonitored]%s", total_peers, online_peers, offline_peers, unmonitored_peers, term);
@@ -6406,6 +6412,7 @@
}
peer_unref(p);
}
+ ao2_iterator_destroy(&i);
}
return res;
@@ -6523,6 +6530,7 @@
astman_append(s, "Status: %s\r\n\r\n", status);
peer_count++;
}
+ ao2_iterator_destroy(&i);
astman_append(s, "Event: PeerlistComplete\r\n%sListItems: %d\r\n\r\n", idtext, peer_count);
return RESULT_SUCCESS;
@@ -7081,6 +7089,7 @@
}
user_unref(user);
}
+ ao2_iterator_destroy(&i);
user = best;
if (!user && !ast_strlen_zero(iaxs[callno]->username)) {
user = realtime_user(iaxs[callno]->username, sin);
@@ -7614,6 +7623,7 @@
}
peer_unref(peer);
}
+ ao2_iterator_destroy(&i);
if (!peer) {
/* We checked our list and didn't find one. It's unlikely, but possible,
that we're trying to authenticate *to* a realtime peer */
@@ -12199,6 +12209,7 @@
}
user_unref(user);
}
+ ao2_iterator_destroy(&i);
}
/* Prune peers who still are supposed to be deleted */
@@ -12214,6 +12225,7 @@
}
peer_unref(peer);
}
+ ao2_iterator_destroy(&i);
}
static void set_config_destroy(void)
@@ -12657,6 +12669,7 @@
iax2_poke_peer(peer, 0);
peer_unref(peer);
}
+ ao2_iterator_destroy(&i);
}
static int reload_config(void)
{
Modified: branches/1.6.1/channels/chan_sip.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/channels/chan_sip.c?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/channels/chan_sip.c (original)
+++ branches/1.6.1/channels/chan_sip.c Mon Oct 5 20:36:36 2009
@@ -13043,7 +13043,6 @@
ast_cli(a->fd, FORMAT, "* Peer name", "In use", "Limit");
i = ao2_iterator_init(peers, 0);
-
while ((peer = ao2_t_iterator_next(&i, "iterate thru peer table"))) {
ao2_lock(peer);
if (peer->call_limit)
@@ -13056,6 +13055,7 @@
ao2_unlock(peer);
unref_peer(peer, "toss iterator pointer");
}
+ ao2_iterator_destroy(&i);
return CLI_SUCCESS;
#undef FORMAT
@@ -13281,6 +13281,7 @@
ao2_unlock(user);
unref_peer(user, "sip show users");
}
+ ao2_iterator_destroy(&user_iter);
if (havepattern)
regfree(®exbuf);
@@ -13469,6 +13470,7 @@
peerarray[total_peers++] = peer;
ao2_unlock(peer);
}
+ ao2_iterator_destroy(&i);
qsort(peerarray, total_peers, sizeof(struct sip_peer *), peercomparefunc);
@@ -13858,6 +13860,7 @@
ao2_unlock(pi);
unref_peer(pi, "toss iterator peer ptr");
}
+ ao2_iterator_destroy(&i);
if (pruned) {
ao2_t_callback(peers, OBJ_NODATA | OBJ_UNLINK | OBJ_MULTIPLE, peer_is_marked, 0,
"initiating callback to remove marked peers");
@@ -14351,6 +14354,7 @@
ao2_unlock(user);
unref_peer(user, "complete sip user");
}
+ ao2_iterator_destroy(&user_iter);
return result;
}
/*! \brief Support routine for 'sip show user' CLI */
@@ -14987,7 +14991,6 @@
}
i = ao2_iterator_init(dialogs, 0);
-
while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
sip_pvt_lock(cur);
if (!strncasecmp(word, cur->callid, wordlen) && ++which > state) {
@@ -14999,6 +15002,7 @@
sip_pvt_unlock(cur);
dialog_unref(cur, "drop ref in iterator loop");
}
+ ao2_iterator_destroy(&i);
return c;
}
@@ -15023,6 +15027,7 @@
break;
}
}
+ ao2_iterator_destroy(&i);
return result;
}
@@ -15032,21 +15037,22 @@
char *result = NULL;
int wordlen = strlen(word);
int which = 0;
- struct ao2_iterator i;
- struct sip_peer *peer;
-
- i = ao2_iterator_init(peers, 0);
- while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
- if (!strncasecmp(word, peer->name, wordlen) &&
- (!flags2 || ast_test_flag(&peer->flags[1], flags2)) &&
- ++which > state && peer->expire > 0)
- result = ast_strdup(peer->name);
- if (result) {
- unref_peer(peer, "toss iterator peer ptr before break");
- break;
- }
- unref_peer(peer, "toss iterator peer ptr");
+ struct ao2_iterator i;
+ struct sip_peer *peer;
+
+ i = ao2_iterator_init(peers, 0);
+ while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
+ if (!strncasecmp(word, peer->name, wordlen) &&
+ (!flags2 || ast_test_flag(&peer->flags[1], flags2)) &&
+ ++which > state && peer->expire > 0)
+ result = ast_strdup(peer->name);
+ if (result) {
+ unref_peer(peer, "toss iterator peer ptr before break");
+ break;
+ }
+ unref_peer(peer, "toss iterator peer ptr");
}
+ ao2_iterator_destroy(&i);
return result;
}
@@ -15132,7 +15138,6 @@
len = strlen(a->argv[3]);
i = ao2_iterator_init(dialogs, 0);
-
while ((cur = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
sip_pvt_lock(cur);
@@ -15212,6 +15217,7 @@
ao2_t_ref(cur, -1, "toss dialog ptr set by iterator_next");
}
+ ao2_iterator_destroy(&i);
if (!found)
ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
@@ -15268,6 +15274,7 @@
sip_pvt_unlock(cur);
ao2_t_ref(cur, -1, "toss dialog ptr from iterator_next");
}
+ ao2_iterator_destroy(&i);
if (!found)
ast_cli(a->fd, "No such SIP Call ID starting with '%s'\n", a->argv[3]);
@@ -20018,7 +20025,6 @@
ignored (or generate errors)
*/
i = ao2_iterator_init(dialogs, 0);
-
while ((p_old = ao2_t_iterator_next(&i, "iterate thru dialogs"))) {
if (p_old == p) {
ao2_t_ref(p_old, -1, "toss dialog ptr from iterator_next before continue");
@@ -20045,6 +20051,7 @@
sip_pvt_unlock(p_old);
ao2_t_ref(p_old, -1, "toss dialog ptr from iterator_next");
}
+ ao2_iterator_destroy(&i);
}
if (!p->expiry)
p->needdestroy = 1;
@@ -23911,11 +23918,10 @@
struct ao2_iterator i;
struct sip_peer *peer;
- i = ao2_iterator_init(peers, 0);
-
if (!speerobjs) /* No peers, just give up */
return;
+ i = ao2_iterator_init(peers, 0);
while ((peer = ao2_t_iterator_next(&i, "iterate thru peers table"))) {
ao2_lock(peer);
ms += 100;
@@ -23926,6 +23932,7 @@
ao2_unlock(peer);
unref_peer(peer, "toss iterator peer ptr");
}
+ ao2_iterator_destroy(&i);
}
/*! \brief Send all known registrations */
@@ -24202,6 +24209,7 @@
ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
ao2_t_ref(p, -1, "toss dialog ptr from iterator_next");
}
+ ao2_iterator_destroy(&i);
ast_mutex_lock(&monlock);
if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
@@ -24218,6 +24226,7 @@
dialog_unlink_all(p, TRUE, TRUE);
ao2_t_ref(p, -1, "throw away iterator result");
}
+ ao2_iterator_destroy(&i);
/* Free memory for local network address mask */
ast_free_ha(localaddr);
Modified: branches/1.6.1/funcs/func_dialgroup.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/funcs/func_dialgroup.c?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/funcs/func_dialgroup.c (original)
+++ branches/1.6.1/funcs/func_dialgroup.c Mon Oct 5 20:36:36 2009
@@ -126,6 +126,7 @@
}
ao2_ref(entry, -1);
}
+ ao2_iterator_destroy(&i);
return res;
}
Modified: branches/1.6.1/include/asterisk/astobj2.h
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/include/asterisk/astobj2.h?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/include/asterisk/astobj2.h (original)
+++ branches/1.6.1/include/asterisk/astobj2.h Mon Oct 5 20:36:36 2009
@@ -593,6 +593,8 @@
... do something on o ...
ao2_ref(o, -1);
}
+
+ ao2_iterator_destroy(&i);
\endcode
The difference with the callback is that the control
@@ -924,15 +926,15 @@
/*! \brief
*
*
- * When we need to walk through a container, we use
+ * When we need to walk through a container, we use an
* ao2_iterator to keep track of the current position.
*
* Because the navigation is typically done without holding the
- * lock on the container across the loop,
- * objects can be inserted or deleted or moved
- * while we work. As a consequence, there is no guarantee that
- * the we manage to touch all the elements on the list, or it
- * is possible that we touch the same object multiple times.
+ * lock on the container across the loop, objects can be inserted or deleted
+ * or moved while we work. As a consequence, there is no guarantee that
+ * we manage to touch all the elements in the container, and it is possible
+ * that we touch the same object multiple times.
+ *
* However, within the current hash table container, the following is true:
* - It is not possible to miss an object in the container while iterating
* unless it gets added after the iteration begins and is added to a bucket
@@ -951,6 +953,10 @@
* ao2_iterator_next() has its refcount incremented,
* and the reference must be explicitly released when done with it.
*
+ * In addition, ao2_iterator_init() will hold a reference to the container
+ * being iterated, which will be freed when ao2_iterator_destroy() is called
+ * to free up the resources used by the iterator (if any).
+ *
* Example:
*
* \code
@@ -966,12 +972,14 @@
* ao2_ref(o, -1);
* }
*
+ * ao2_iterator_destroy(&i);
+ *
* \endcode
*
*/
/*! \brief
- * The Astobj2 iterator
+ * The astobj2 iterator
*
* \note You are not supposed to know the internals of an iterator!
* We would like the iterator to be opaque, unfortunately
@@ -987,21 +995,20 @@
* - a bucket number;
* - the object_id, which is also the container version number
* when the object was inserted. This identifies the object
- * univoquely, however reaching the desired object requires
+ * uniquely, however reaching the desired object requires
* scanning a list.
* - a pointer, and a container version when we saved the pointer.
* If the container has not changed its version number, then we
* can safely follow the pointer to reach the object in constant time.
*
* Details are in the implementation of ao2_iterator_next()
- * A freshly-initialized iterator has bucket=0, version = 0.
+ * A freshly-initialized iterator has bucket=0, version=0.
*/
struct ao2_iterator {
/*! the container */
struct ao2_container *c;
/*! operation flags */
int flags;
-#define F_AO2I_DONTLOCK 1 /*!< don't lock when iterating */
/*! current bucket */
int bucket;
/*! container version */
@@ -1012,11 +1019,46 @@
unsigned int version;
};
-/* the flags field can contain F_AO2I_DONTLOCK, which will prevent
- ao2_iterator_next calls from locking the container while it
- searches for the next pointer */
-
+/*! Flags that can be passed to ao2_iterator_init() to modify the behavior
+ * of the iterator.
+ */
+enum ao2_iterator_flags {
+ /*! Prevents ao2_iterator_next() from locking the container
+ * while retrieving the next object from it.
+ */
+ AO2_ITERATOR_DONTLOCK = (1 << 0),
+};
+
+/*!
+ * \brief Create an iterator for a container
+ *
+ * \param c the container
+ * \param flags one or more flags from ao2_iterator_flags
+ *
+ * \retval the constructed iterator
+ *
+ * \note This function does \b not take a pointer to an iterator;
+ * rather, it returns an iterator structure that should be
+ * assigned to (overwriting) an existing iterator structure
+ * allocated on the stack or on the heap.
+ *
+ * This function will take a reference on the container being iterated.
+ *
+ */
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags);
+
+/*!
+ * \brief Destroy a container iterator
+ *
+ * \param i the iterator to destroy
+ *
+ * \retval none
+ *
+ * This function will release the container reference held by the iterator
+ * and any other resources it may be holding.
+ *
+ */
+void ao2_iterator_destroy(struct ao2_iterator *i);
#ifdef REF_DEBUG
Modified: branches/1.6.1/main/astobj2.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/main/astobj2.c?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/main/astobj2.c (original)
+++ branches/1.6.1/main/astobj2.c Mon Oct 5 20:36:36 2009
@@ -734,8 +734,19 @@
.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;
}
/*
@@ -752,7 +763,7 @@
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
@@ -808,7 +819,7 @@
_ao2_ref_debug(ret, 1, tag, file, line, funcname);
}
- if (!(a->flags & F_AO2I_DONTLOCK))
+ if (!(a->flags & AO2_ITERATOR_DONTLOCK))
ao2_unlock(a->c);
return ret;
@@ -826,7 +837,7 @@
_ao2_ref(ret, 1);
}
- if (!(a->flags & F_AO2I_DONTLOCK))
+ if (!(a->flags & AO2_ITERATOR_DONTLOCK))
ao2_unlock(a->c);
return ret;
Modified: branches/1.6.1/res/res_musiconhold.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/res/res_musiconhold.c?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/res/res_musiconhold.c (original)
+++ branches/1.6.1/res/res_musiconhold.c Mon Oct 5 20:36:36 2009
@@ -1571,7 +1571,6 @@
return CLI_SHOWUSAGE;
i = ao2_iterator_init(mohclasses, 0);
-
for (; (class = ao2_t_iterator_next(&i, "Show files iterator")); mohclass_unref(class, "Unref iterator in moh show files")) {
int x;
@@ -1584,6 +1583,7 @@
ast_cli(a->fd, "\tFile: %s\n", class->filearray[x]);
}
}
+ ao2_iterator_destroy(&i);
return CLI_SUCCESS;
}
@@ -1608,7 +1608,6 @@
return CLI_SHOWUSAGE;
i = ao2_iterator_init(mohclasses, 0);
-
for (; (class = ao2_t_iterator_next(&i, "Show classes iterator")); mohclass_unref(class, "Unref iterator in moh show classes")) {
ast_cli(a->fd, "Class: %s\n", class->name);
ast_cli(a->fd, "\tMode: %s\n", S_OR(class->mode, "<none>"));
@@ -1620,6 +1619,7 @@
ast_cli(a->fd, "\tFormat: %s\n", ast_getformatname(class->format));
}
}
+ ao2_iterator_destroy(&i);
return CLI_SUCCESS;
}
Modified: branches/1.6.1/res/res_odbc.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/res/res_odbc.c?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/res/res_odbc.c (original)
+++ branches/1.6.1/res/res_odbc.c Mon Oct 5 20:36:36 2009
@@ -564,6 +564,7 @@
break;
}
}
+ ao2_iterator_destroy(&aoi);
if (!ret && !strncasecmp(a->word, "all", length) && ++which > a->n) {
ret = ast_strdup("all");
}
@@ -598,6 +599,7 @@
ast_mutex_unlock(¤t->lock);
ao2_ref(current, -1);
}
+ ao2_iterator_destroy(&aoi2);
} else {
/* Should only ever be one of these */
struct ao2_iterator aoi2 = ao2_iterator_init(class->obj_container, 0);
@@ -605,11 +607,13 @@
ast_cli(a->fd, " Pooled: No\n Connected: %s\n", current->up && ast_odbc_sanity_check(current) ? "Yes" : "No");
ao2_ref(current, -1);
}
+ ao2_iterator_destroy(&aoi2);
}
ast_cli(a->fd, "\n");
}
ao2_ref(class, -1);
}
+ ao2_iterator_destroy(&aoi);
return CLI_SUCCESS;
}
@@ -880,6 +884,7 @@
class->delme = 1;
ao2_ref(class, -1);
}
+ ao2_iterator_destroy(&aoi);
load_odbc_config();
@@ -919,6 +924,7 @@
* b) the object has already been destroyed.
*/
}
+ ao2_iterator_destroy(&aoi2);
ao2_unlink(class_container, class); /* unlink C-ref from container (reference handled implicitly) */
/* At this point, either
* a) there's an outstanding O-ref, which holds an outstanding C-ref, or
@@ -928,6 +934,7 @@
}
ao2_ref(class, -1); /* C-ref-- (by iterator) */
}
+ ao2_iterator_destroy(&aoi);
/* Empty the cache; it will get rebuilt the next time the tables are needed. */
AST_RWLIST_WRLOCK(&odbc_tables);
Modified: branches/1.6.1/res/res_phoneprov.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.6.1/res/res_phoneprov.c?view=diff&rev=222186&r1=222185&r2=222186
==============================================================================
--- branches/1.6.1/res/res_phoneprov.c (original)
+++ branches/1.6.1/res/res_phoneprov.c Mon Oct 5 20:36:36 2009
@@ -829,6 +829,7 @@
ao2_unlink(users, user);
user = unref_user(user);
}
+ ao2_iterator_destroy(&i);
}
/*! \brief Build and return a user structure based on gathered config data */
@@ -1065,6 +1066,7 @@
ao2_unlink(http_routes, route);
route = unref_route(route);
}
+ ao2_iterator_destroy(&i);
}
/*! \brief Delete all phone profiles, freeing their memory */
@@ -1078,6 +1080,7 @@
ao2_unlink(profiles, profile);
profile = unref_profile(profile);
}
+ ao2_iterator_destroy(&i);
}
/*! \brief A dialplan function that can be used to print a string for each phoneprov user */
@@ -1105,6 +1108,7 @@
ast_build_string(&buf, &len, "%s", expand_buf);
user = unref_user(user);
}
+ ao2_iterator_destroy(&i);
return 0;
}
@@ -1212,6 +1216,7 @@
ast_cli(a->fd, FORMAT, route->uri, route->file->template);
route = unref_route(route);
}
+ ao2_iterator_destroy(&i);
ast_cli(a->fd, "\nDynamic routes\n\n");
ast_cli(a->fd, FORMAT, "Relative URI", "Template");
@@ -1222,6 +1227,7 @@
ast_cli(a->fd, FORMAT, route->uri, route->file->template);
route = unref_route(route);
}
+ ao2_iterator_destroy(&i);
return CLI_SUCCESS;
}
More information about the asterisk-commits
mailing list