From 36e1e0f5204b401ee5a4a56e9190940cead5993a Mon Sep 17 00:00:00 2001
From: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Date: Fri, 23 Aug 2013 17:36:45 -0400
Subject: [PATCH] * #28529: daemon: halve boolean port array

Since we force RTP to be on even ports, we only need half as many
booleans to keep track of what ports are in use.
---
 daemon/src/sip/sipaccount.cpp | 9 +++++----
 daemon/src/sip/sipaccount.h   | 3 ++-
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/daemon/src/sip/sipaccount.cpp b/daemon/src/sip/sipaccount.cpp
index 3e25e38af8..45e0fc06a9 100644
--- a/daemon/src/sip/sipaccount.cpp
+++ b/daemon/src/sip/sipaccount.cpp
@@ -66,7 +66,8 @@ const char *const TRUE_STR = "true";
 const char *const FALSE_STR = "false";
 }
 
-bool SIPAccount::portsInUse_[MAX_PORT];
+// we force RTP ports to be even, so we only need HALF_MAX_PORT booleans
+bool SIPAccount::portsInUse_[HALF_MAX_PORT];
 
 SIPAccount::SIPAccount(const std::string& accountID)
     : Account(accountID)
@@ -1396,16 +1397,16 @@ SIPAccount::getRandomEvenNumber(const std::pair<uint16_t, uint16_t> &range)
     uint16_t result;
     do {
         result = 2 * (halfLower + rand() % (halfUpper - halfLower + 1));
-    } while (portsInUse_[result]);
+    } while (portsInUse_[result / 2]);
 
-    portsInUse_[result] = true;
+    portsInUse_[result / 2] = true;
     return result;
 }
 
 void
 SIPAccount::releasePort(uint16_t port)
 {
-    portsInUse_[port] = false;
+    portsInUse_[port / 2] = false;
 }
 
 uint16_t
diff --git a/daemon/src/sip/sipaccount.h b/daemon/src/sip/sipaccount.h
index 096af78c0f..7cf564fadd 100644
--- a/daemon/src/sip/sipaccount.h
+++ b/daemon/src/sip/sipaccount.h
@@ -105,6 +105,7 @@ class SIPVoIPLink;
  * @brief A SIP Account specify SIP specific functions and object = SIPCall/SIPVoIPLink)
  */
 enum {MAX_PORT = 65536};
+enum {HALF_MAX_PORT = MAX_PORT / 2};
 
 class SIPAccount : public Account {
     public:
@@ -789,7 +790,7 @@ class SIPAccount : public Account {
          */
         std::pair<uint16_t, uint16_t> videoPortRange_;
 #endif
-        static bool portsInUse_[MAX_PORT];
+        static bool portsInUse_[HALF_MAX_PORT];
         static uint16_t getRandomEvenNumber(const std::pair<uint16_t, uint16_t> &range);
 
 };
-- 
GitLab