mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-18 22:18:19 +00:00
97 lines
1.8 KiB
C++
97 lines
1.8 KiB
C++
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
|
|
|
#include "snap_id_pool.h"
|
|
|
|
#include <base/system.h>
|
|
|
|
CSnapIDPool::CSnapIDPool()
|
|
{
|
|
Reset();
|
|
}
|
|
|
|
void CSnapIDPool::Reset()
|
|
{
|
|
for(int i = 0; i < MAX_IDS; i++)
|
|
{
|
|
m_aIDs[i].m_Next = i + 1;
|
|
m_aIDs[i].m_State = ID_FREE;
|
|
}
|
|
|
|
m_aIDs[MAX_IDS - 1].m_Next = -1;
|
|
m_FirstFree = 0;
|
|
m_FirstTimed = -1;
|
|
m_LastTimed = -1;
|
|
m_Usage = 0;
|
|
m_InUsage = 0;
|
|
}
|
|
|
|
void CSnapIDPool::RemoveFirstTimeout()
|
|
{
|
|
int NextTimed = m_aIDs[m_FirstTimed].m_Next;
|
|
|
|
// add it to the free list
|
|
m_aIDs[m_FirstTimed].m_Next = m_FirstFree;
|
|
m_aIDs[m_FirstTimed].m_State = ID_FREE;
|
|
m_FirstFree = m_FirstTimed;
|
|
|
|
// remove it from the timed list
|
|
m_FirstTimed = NextTimed;
|
|
if(m_FirstTimed == -1)
|
|
m_LastTimed = -1;
|
|
|
|
m_Usage--;
|
|
}
|
|
|
|
int CSnapIDPool::NewID()
|
|
{
|
|
int64_t Now = time_get();
|
|
|
|
// process timed ids
|
|
while(m_FirstTimed != -1 && m_aIDs[m_FirstTimed].m_Timeout < Now)
|
|
RemoveFirstTimeout();
|
|
|
|
int ID = m_FirstFree;
|
|
if(ID == -1)
|
|
{
|
|
dbg_msg("server", "invalid id");
|
|
return ID;
|
|
}
|
|
m_FirstFree = m_aIDs[m_FirstFree].m_Next;
|
|
m_aIDs[ID].m_State = ID_ALLOCATED;
|
|
m_Usage++;
|
|
m_InUsage++;
|
|
return ID;
|
|
}
|
|
|
|
void CSnapIDPool::TimeoutIDs()
|
|
{
|
|
// process timed ids
|
|
while(m_FirstTimed != -1)
|
|
RemoveFirstTimeout();
|
|
}
|
|
|
|
void CSnapIDPool::FreeID(int ID)
|
|
{
|
|
if(ID < 0)
|
|
return;
|
|
dbg_assert((size_t)ID < std::size(m_aIDs), "id is out of range");
|
|
dbg_assert(m_aIDs[ID].m_State == ID_ALLOCATED, "id is not allocated");
|
|
|
|
m_InUsage--;
|
|
m_aIDs[ID].m_State = ID_TIMED;
|
|
m_aIDs[ID].m_Timeout = time_get() + time_freq() * 5;
|
|
m_aIDs[ID].m_Next = -1;
|
|
|
|
if(m_LastTimed != -1)
|
|
{
|
|
m_aIDs[m_LastTimed].m_Next = ID;
|
|
m_LastTimed = ID;
|
|
}
|
|
else
|
|
{
|
|
m_FirstTimed = ID;
|
|
m_LastTimed = ID;
|
|
}
|
|
}
|