]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/DescriptorSet.cc
2 * DEBUG: section 05 Comm
5 #include "globals.h" /* for Squid_MaxFD */
6 #include "DescriptorSet.h"
8 // pre-allocates descriptor store and index for Squid_MaxFD descriptors
9 DescriptorSet::DescriptorSet(): descriptors_(NULL
), index_(NULL
),
10 capacity_(0), size_(0)
12 // we allocate once and never realloc, at least for now
13 capacity_
= Squid_MaxFD
;
14 descriptors_
= new int[capacity_
];
15 index_
= new int[capacity_
];
17 // fill index with -1s to be able to say whether a descriptor is present
18 // it is not essential to fill the descriptors, but it enables more checks
19 for (int i
= 0; i
< capacity_
; ++i
)
20 index_
[i
] = descriptors_
[i
] = -1;
23 DescriptorSet::~DescriptorSet()
25 delete[] descriptors_
;
29 /// adds if unique; returns true if added
31 DescriptorSet::add(int fd
)
33 assert(0 <= fd
&& fd
< capacity_
); // \todo: replace with Must()
36 return false; // already have it
38 assert(size_
< capacity_
); // \todo: replace with Must()
39 const int pos
= size_
;
42 descriptors_
[pos
] = fd
;
43 return true; // really added
46 /// deletes if there; returns true if deleted
48 DescriptorSet::del(int fd
)
50 assert(0 <= fd
&& fd
< capacity_
); // \todo: here and below, use Must()
53 return false; // we do not have it
56 const int delPos
= index_
[fd
];
57 assert(0 <= delPos
&& delPos
< capacity_
);
59 // move the last descriptor to the deleted fd position
60 // to avoid skipping deleted descriptors in pop()
61 const int lastPos
= size_
-1;
62 const int lastFd
= descriptors_
[lastPos
];
63 assert(delPos
<= lastPos
); // may be the same
64 descriptors_
[delPos
] = lastFd
;
65 index_
[lastFd
] = delPos
;
67 descriptors_
[lastPos
] = -1;
71 return true; // really added
74 /// ejects one descriptor in unspecified order
79 const int lastPos
=--size_
;
80 const int lastFd
= descriptors_
[lastPos
];
81 assert(0 <= lastFd
&& lastFd
< capacity_
);
84 descriptors_
[lastPos
] = -1;
91 DescriptorSet::print(std::ostream
&os
) const
93 // \todo add "name" if the set is used for more than just half-closed FDs
94 os
<< size_
<< " FDs";