Skip to content

Commit

Permalink
[core] Fixed sender list to reallocate on insert
Browse files Browse the repository at this point in the history
if required
  • Loading branch information
maxsharabayko authored and rndi committed Aug 27, 2019
1 parent 6ef6c22 commit ae46ce2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
79 changes: 42 additions & 37 deletions srtcore/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,32 +273,6 @@ CSndUList::~CSndUList()
pthread_mutex_destroy(&m_ListLock);
}

void CSndUList::insert(int64_t ts, const CUDT* u)
{
CGuard listguard(m_ListLock);

// increase the heap array size if necessary
if (m_iLastEntry == m_iArrayLength - 1)
{
CSNode** temp = NULL;

try
{
temp = new CSNode*[m_iArrayLength * 2];
}
catch(...)
{
return;
}

memcpy(temp, m_pHeap, sizeof(CSNode*) * m_iArrayLength);
m_iArrayLength *= 2;
delete [] m_pHeap;
m_pHeap = temp;
}

insert_(ts, u);
}

void CSndUList::update(const CUDT* u, EReschedule reschedule)
{
Expand All @@ -319,6 +293,8 @@ void CSndUList::update(const CUDT* u, EReschedule reschedule)
}

remove_(u);
insert_norealloc(1, u);
return;
}

insert_(1, u);
Expand Down Expand Up @@ -366,7 +342,7 @@ int CSndUList::pop(sockaddr*& addr, CPacket& pkt)

// insert a new entry, ts is the next processing time
if (ts > 0)
insert_(ts, u);
insert_norealloc(ts, u);

return 1;
}
Expand All @@ -388,14 +364,47 @@ uint64_t CSndUList::getNextProcTime()
return m_pHeap[0]->m_llTimeStamp_tk;
}


void CSndUList::realloc_()
{
CSNode** temp = NULL;

try
{
temp = new CSNode * [m_iArrayLength * 2];
}
catch (...)
{
return;
}

memcpy(temp, m_pHeap, sizeof(CSNode*) * m_iArrayLength);
m_iArrayLength *= 2;
delete[] m_pHeap;
m_pHeap = temp;
}


void CSndUList::insert_(int64_t ts, const CUDT* u)
{
// increase the heap array size if necessary
if (m_iLastEntry == m_iArrayLength - 1)
realloc_();

insert_norealloc(ts, u);
}


void CSndUList::insert_norealloc(int64_t ts, const CUDT* u)
{
CSNode* n = u->m_pSNode;

// do not insert repeated node
if (n->m_iHeapLoc >= 0)
return;

SRT_ASSERT(m_iLastEntry < m_iArrayLength);

m_iLastEntry ++;
m_pHeap[m_iLastEntry] = n;
n->m_llTimeStamp_tk = ts;
Expand All @@ -405,16 +414,12 @@ void CSndUList::insert_(int64_t ts, const CUDT* u)
while (p != 0)
{
p = (q - 1) >> 1;
if (m_pHeap[p]->m_llTimeStamp_tk > m_pHeap[q]->m_llTimeStamp_tk)
{
CSNode* t = m_pHeap[p];
m_pHeap[p] = m_pHeap[q];
m_pHeap[q] = t;
t->m_iHeapLoc = q;
q = p;
}
else
break;
if (m_pHeap[p]->m_llTimeStamp_tk <= m_pHeap[q]->m_llTimeStamp_tk)
break;

swap(m_pHeap[p], m_pHeap[q]);
m_pHeap[q]->m_iHeapLoc = q;
q = p;
}

n->m_iHeapLoc = q;
Expand Down
23 changes: 17 additions & 6 deletions srtcore/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,6 @@ friend class CSndQueue;

static EReschedule rescheduleIf(bool cond) { return cond ? DO_RESCHEDULE : DONT_RESCHEDULE; }

/// Insert a new UDT instance into the list.
/// @param [in] ts time stamp: next processing time
/// @param [in] u pointer to the UDT instance

void insert(int64_t ts, const CUDT* u);

/// Update the timestamp of the UDT instance on the list.
/// @param [in] u pointer to the UDT instance
/// @param [in] resechedule if the timestampe shoudl be rescheduled
Expand All @@ -196,7 +190,24 @@ friend class CSndQueue;
uint64_t getNextProcTime();

private:

/// Doubles the size of the list.
///
void realloc_();

/// Insert a new UDT instance into the list with realloc if required.
///
/// @param [in] ts time stamp: next processing time
/// @param [in] u pointer to the UDT instance
void insert_(int64_t ts, const CUDT* u);

/// Insert a new UDT instance into the list without realloc.
/// Should be called if there is a gauranteed space for the element.
///
/// @param [in] ts time stamp: next processing time
/// @param [in] u pointer to the UDT instance
void insert_norealloc(int64_t ts, const CUDT* u);

void remove_(const CUDT* u);

private:
Expand Down

0 comments on commit ae46ce2

Please sign in to comment.