From c785aa2305cfa26a934e9e07fedfaa124487857d Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Mon, 7 Jul 2008 18:13:17 +0000 Subject: [PATCH] use lighter math and avoid infinite loop in port allocator (FSCORE-148) git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8909 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/switch_core_port_allocator.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/switch_core_port_allocator.c b/src/switch_core_port_allocator.c index 4f0f19833c..feff7c3970 100644 --- a/src/switch_core_port_allocator.c +++ b/src/switch_core_port_allocator.c @@ -121,23 +121,20 @@ SWITCH_DECLARE(switch_status_t) switch_core_port_allocator_request_port(switch_c srand(getpid() + (unsigned) switch_timestamp(NULL)); while (alloc->track_used < alloc->track_len) { - double r; uint32_t index; - int tries = 0; + uint32_t tries = 0; - do { - r = ((double) rand() / ((double) (RAND_MAX) + (double) (1))); - index = (int) (r * alloc->track_len); + /* randomly pick a port */ + index = rand() % alloc->track_len; + + /* if it is used walk up the list to find a free one */ + while (alloc->track[index] && tries < alloc->track_len) + { tries++; - } while ((alloc->track[index] || index >= alloc->track_len) && tries < 10000); - - while (alloc->track[index]) { - if (++index >= alloc->track_len) { - index = 0; - } + if(++index >= alloc->track_len) index = 0; } - if (index < alloc->track_len) { + if (tries < alloc->track_len) { alloc->track[index] = 1; alloc->track_used++; status = SWITCH_STATUS_SUCCESS;