00001 // -*- c++ -*- 00002 //------------------------------------------------------------------------------ 00003 // IdSet.cpp 00004 //------------------------------------------------------------------------------ 00005 // Copyright (C) 1997-2002 Vladislav Grinchenko 00006 // 00007 // This library is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU Library General Public 00009 // License as published by the Free Software Foundation; either 00010 // version 2 of the License, or (at your option) any later version. 00011 //------------------------------------------------------------------------------ 00012 00013 #include "assa/Logger.h" 00014 #include "assa/IdSet.h" 00015 00016 using namespace ASSA; 00017 00018 int 00019 IdSet:: 00020 newid() 00021 { 00022 register int i; 00023 register int current; 00024 00025 trace("IdSet::newid"); 00026 00027 current = m_next_available_id++; 00028 00029 if (m_next_available_id < FD_SETSIZE) 00030 { 00031 // mark current id as being in use 00032 FD_SET(current, &m_id_set_map); 00033 00034 // search starting from current position to the end 00035 // assuming that m_next_available_id is maintained 00036 // to be the lowest available at all times. 00037 00038 for (i=current+1; i<FD_SETSIZE; i++) 00039 { 00040 if (!FD_ISSET(i, &m_id_set_map)) 00041 { 00042 m_next_available_id = i; 00043 return current; 00044 } 00045 } 00046 // if I am here, I am out of ids 00047 m_next_available_id = FD_SETSIZE; 00048 } 00049 return -1; 00050 } 00051 00052 int 00053 IdSet:: 00054 recycle(int id_) 00055 { 00056 trace("IdSet::recycle"); 00057 00058 if ( 0 <= id_ && id_ < FD_SETSIZE ) { 00059 FD_CLR(id_, &m_id_set_map); // mark id as free 00060 00061 // if id is smaller then current, adjust current 00062 if (id_ < m_next_available_id) { 00063 m_next_available_id = id_; 00064 } 00065 return 0; 00066 } 00067 return -1; 00068 }