Module: sems
Branch: master
Commit: 9cba728036a0b8a60e2d893d0053d5990b45e92f
URL:    
http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=9cba728036a0b8a60e2d893d0053d5990b45e92f

Author: Raphael Coeffic <[email protected]>
Committer: Raphael Coeffic <[email protected]>
Date:   Mon Sep 10 17:25:21 2012 +0200

rtp: speed up packet cache

---

 core/AmRtpStream.cpp |   36 ++++++++++++++++++++++++------------
 core/AmRtpStream.h   |    8 +++++++-
 2 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/core/AmRtpStream.cpp b/core/AmRtpStream.cpp
index f5c115d..0af417d 100644
--- a/core/AmRtpStream.cpp
+++ b/core/AmRtpStream.cpp
@@ -1000,28 +1000,40 @@ string AmRtpStream::getPayloadName(int payload_type)
   return string("");
 }
 
-
-PacketMem::PacketMem() {
+PacketMem::PacketMem()
+  : cur_idx(0), n_used(0)
+{
   memset(used, 0, sizeof(used));
 }
 
-inline AmRtpPacket* PacketMem::newPacket() {
-  for (int i=0;i<MAX_PACKETS;i++)
-    if (!used[i]) {
-      used[i]=true;
-      return &packets[i];
-    }
-  
-  return NULL;
+inline AmRtpPacket* PacketMem::newPacket() 
+{
+  if(n_used == MAX_PACKETS)
+    return NULL; // full
+
+  while(used[cur_idx])
+    cur_idx = (cur_idx + 1) & MAX_PACKETS_MASK;
+
+  used[cur_idx] = true;
+  n_used++;
+
+  AmRtpPacket* p = &packets[cur_idx];
+  cur_idx = (cur_idx + 1) & MAX_PACKETS_MASK;
+
+  return p;
 }
 
-inline void PacketMem::freePacket(AmRtpPacket* p) {
+inline void PacketMem::freePacket(AmRtpPacket* p) 
+{
   if (!p)  return;
 
   used[p-packets] = false;
+  n_used--;
 }
 
-inline void PacketMem::clear() {
+inline void PacketMem::clear() 
+{
   memset(used, 0, sizeof(used));
+  n_used = cur_idx = 0;
 }
  
diff --git a/core/AmRtpStream.h b/core/AmRtpStream.h
index 88a7a50..9764487 100644
--- a/core/AmRtpStream.h
+++ b/core/AmRtpStream.h
@@ -68,7 +68,9 @@ struct amci_payload_t;
  * This provides the memory for the receive buffer.
  */
 struct PacketMem {
-#define MAX_PACKETS 32
+#define MAX_PACKETS_BITS 5
+#define MAX_PACKETS (1<<MAX_PACKETS_BITS)
+#define MAX_PACKETS_MASK (MAX_PACKETS-1)
 
   AmRtpPacket packets[MAX_PACKETS];
   bool        used[MAX_PACKETS];
@@ -78,6 +80,10 @@ struct PacketMem {
   inline AmRtpPacket* newPacket();
   inline void freePacket(AmRtpPacket* p);
   inline void clear();
+
+private:
+  unsigned int cur_idx;
+  unsigned int n_used;
 };
 
 /** \brief event fired on RTP timeout */

_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to