[Freeswitch-svn] [commit] r8909 - freeswitch/trunk/src

Freeswitch SVN mikej at freeswitch.org
Mon Jul 7 14:13:18 EDT 2008


Author: mikej
Date: Mon Jul  7 14:13:17 2008
New Revision: 8909

Modified:
   freeswitch/trunk/src/switch_core_port_allocator.c

Log:
use lighter math and avoid infinite loop in port allocator  (FSCORE-148)

Modified: freeswitch/trunk/src/switch_core_port_allocator.c
==============================================================================
--- freeswitch/trunk/src/switch_core_port_allocator.c	(original)
+++ freeswitch/trunk/src/switch_core_port_allocator.c	Mon Jul  7 14:13:17 2008
@@ -121,23 +121,20 @@
 	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);
-			tries++;
-		} while ((alloc->track[index] || index >= alloc->track_len) && tries < 10000);
+		/* randomly pick a port */
+		index = rand() % alloc->track_len;
 
-		while (alloc->track[index]) {
-			if (++index >= alloc->track_len) {
-				index = 0;
-			}
+		/* if it is used walk up the list to find a free one */
+		while (alloc->track[index] && tries < alloc->track_len)
+		{
+			tries++;
+			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;



More information about the Freeswitch-svn mailing list