Loading...
Searching...
No Matches
IdSet.cpp
Go to the documentation of this file.
// -*- c++ -*-
//------------------------------------------------------------------------------
// IdSet.cpp
//------------------------------------------------------------------------------
// Copyright (C) 1997-2002 Vladislav Grinchenko
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
#include "assa/Logger.h"
#include "assa/IdSet.h"
using namespace ASSA;
int
IdSet::
newid()
{
register int i;
register int current;
trace("IdSet::newid");
current = m_next_available_id++;
if (m_next_available_id < FD_SETSIZE)
{
// mark current id as being in use
FD_SET(current, &m_id_set_map);
// search starting from current position to the end
// assuming that m_next_available_id is maintained
// to be the lowest available at all times.
for (i=current+1; i<FD_SETSIZE; i++)
{
if (!FD_ISSET(i, &m_id_set_map))
{
m_next_available_id = i;
return current;
}
}
// if I am here, I am out of ids
m_next_available_id = FD_SETSIZE;
}
return -1;
}
int
IdSet::
recycle(int id_)
{
trace("IdSet::recycle");
if ( 0 <= id_ && id_ < FD_SETSIZE ) {
FD_CLR(id_, &m_id_set_map); // mark id as free
// if id is smaller then current, adjust current
if (id_ < m_next_available_id) {
m_next_available_id = id_;
}
return 0;
}
return -1;
}
IdSet.h
Class IdSet implements a set of reusable unique IDs, up to 1024.
Logger.h
An abstraction to message logging facility.
trace
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition Logger.h:429
ASSA::AutoPtrRef
A wrapper class to provide AutoPtr with reference semantics.
Definition AutoPtr.h:32
ASSA::IdSet::m_next_available_id
int m_next_available_id
Current id.
Definition IdSet.h:62
ASSA::IdSet::newid
int newid()
Return current id.
Definition IdSet.cpp:20
ASSA::IdSet::recycle
int recycle(int id_)
Recycle id_.
Definition IdSet.cpp:54
ASSA::IdSet::m_id_set_map
fd_set m_id_set_map
Map of all ids.
Definition IdSet.h:66
ASSA
Definition Acceptor.h:40