libassa  3.5.1
IdSet.cpp
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // IdSet.cpp
4 //------------------------------------------------------------------------------
5 // Copyright (C) 1997-2002 Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 
13 #include "assa/Logger.h"
14 #include "assa/IdSet.h"
15 
16 using namespace ASSA;
17 
18 int
20 newid()
21 {
22  register int i;
23  register int current;
24 
25  trace("IdSet::newid");
26 
27  current = m_next_available_id++;
28 
29  if (m_next_available_id < FD_SETSIZE)
30  {
31  // mark current id as being in use
32  FD_SET(current, &m_id_set_map);
33 
34  // search starting from current position to the end
35  // assuming that m_next_available_id is maintained
36  // to be the lowest available at all times.
37 
38  for (i=current+1; i<FD_SETSIZE; i++)
39  {
40  if (!FD_ISSET(i, &m_id_set_map))
41  {
43  return current;
44  }
45  }
46  // if I am here, I am out of ids
47  m_next_available_id = FD_SETSIZE;
48  }
49  return -1;
50 }
51 
52 int
54 recycle(int id_)
55 {
56  trace("IdSet::recycle");
57 
58  if ( 0 <= id_ && id_ < FD_SETSIZE ) {
59  FD_CLR(id_, &m_id_set_map); // mark id as free
60 
61  // if id is smaller then current, adjust current
62  if (id_ < m_next_available_id) {
63  m_next_available_id = id_;
64  }
65  return 0;
66  }
67  return -1;
68 }
Class IdSet implements a set of reusable unique IDs, up to 1024.
An abstraction to message logging facility.
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition: Logger.h:429
int m_next_available_id
Current id.
Definition: IdSet.h:62
int newid()
Return current id.
Definition: IdSet.cpp:20
int recycle(int id_)
Recycle id_.
Definition: IdSet.cpp:54
fd_set m_id_set_map
Map of all ids.
Definition: IdSet.h:66
Definition: Acceptor.h:40