DescriptorSet.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 05 Comm */
10
11#include "squid.h"
12#include "DescriptorSet.h"
13#include "globals.h" /* for Squid_MaxFD */
14
15// pre-allocates descriptor store and index for Squid_MaxFD descriptors
16DescriptorSet::DescriptorSet(): descriptors_(nullptr), index_(nullptr),
17 capacity_(0), size_(0)
18{
19 // we allocate once and never realloc, at least for now
21 descriptors_ = new int[capacity_];
22 index_ = new int[capacity_];
23
24 // fill index with -1s to be able to say whether a descriptor is present
25 // it is not essential to fill the descriptors, but it enables more checks
26 for (int i = 0; i < capacity_; ++i)
27 index_[i] = descriptors_[i] = -1;
28}
29
31{
32 delete[] descriptors_;
33 delete[] index_;
34}
35
37bool
39{
40 assert(0 <= fd && fd < capacity_); // TODO: replace with Must()
41
42 if (has(fd))
43 return false; // already have it
44
45 assert(size_ < capacity_); // TODO: replace with Must()
46 const int pos = size_;
47 ++size_;
48 index_[fd] = pos;
49 descriptors_[pos] = fd;
50 return true; // really added
51}
52
54bool
56{
57 assert(0 <= fd && fd < capacity_); // TODO: here and below, use Must()
58
59 if (!has(fd))
60 return false; // we do not have it
61
62 assert(!empty());
63 const int delPos = index_[fd];
64 assert(0 <= delPos && delPos < capacity_);
65
66 // move the last descriptor to the deleted fd position
67 // to avoid skipping deleted descriptors in pop()
68 const int lastPos = size_-1;
69 const int lastFd = descriptors_[lastPos];
70 assert(delPos <= lastPos); // may be the same
71 descriptors_[delPos] = lastFd;
72 index_[lastFd] = delPos;
73
74 descriptors_[lastPos] = -1;
75 index_[fd] = -1;
76 --size_;
77
78 return true; // really added
79}
80
82int
84{
85 assert(!empty());
86 const int lastPos =--size_;
87 const int lastFd = descriptors_[lastPos];
88 assert(0 <= lastFd && lastFd < capacity_);
89
90 // cleanup
91 descriptors_[lastPos] = -1;
92 index_[lastFd] = -1;
93
94 return lastFd;
95}
96
97void
98DescriptorSet::print(std::ostream &os) const
99{
100 // TODO: add "name" if the set is used for more than just half-closed FDs
101 os << size_ << " FDs";
102}
103
#define assert(EX)
Definition: assert.h:17
int pop()
deletes and returns one descriptor, in unspecified order
bool del(int fd)
deletes if there; returns true if deleted
int * index_
descriptor:position index into descriptors_
Definition: DescriptorSet.h:53
bool empty() const
number of descriptors in the set
Definition: DescriptorSet.h:37
int * descriptors_
descriptor values in random order
Definition: DescriptorSet.h:52
int size_
number of descriptors in the set
Definition: DescriptorSet.h:55
bool has(const int fd) const
checks whether fd is in the set
Definition: DescriptorSet.h:28
void print(std::ostream &os) const
outputs debugging info about the set
int capacity_
total number of descriptor slots
Definition: DescriptorSet.h:54
bool add(int fd)
adds if unique; returns true if added
int Squid_MaxFD

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors