GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/fd_table.h
Date: 2026-06-28 02:36:10
Exec Total Coverage
Lines: 62 63 98.4%
Branches: 40 59 67.8%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #ifndef CVMFS_FD_TABLE_H_
6 #define CVMFS_FD_TABLE_H_
7
8 #include <errno.h>
9 #include <inttypes.h>
10 #include <stdint.h>
11
12 #include <cassert>
13 #include <cstddef>
14 #include <vector>
15
16 #include "util/single_copy.h"
17
18 /**
19 * Maintains integers mapped to custom open file descriptors. File descriptors
20 * can be added, removed, and accessed. All operations take constant time. The
21 * maximum file descriptor number needs to be known upfront.
22 *
23 * Note that new file descriptors do not necessarily have the smallest available
24 * number but any number between 0..max_open_fds.
25 *
26 * This class is used by a couple of cache managers.
27 */
28 template<class HandleT>
29 class FdTable : SingleCopy {
30 public:
31 1904 FdTable(unsigned max_open_fds, const HandleT &invalid_handle)
32 1904 : invalid_handle_(invalid_handle)
33 1904 , fd_pivot_(0)
34
1/2
✓ Branch 2 taken 1904 times.
✗ Branch 3 not taken.
1904 , fd_index_(max_open_fds)
35
4/7
✓ Branch 3 taken 96 times.
✓ Branch 4 taken 1808 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 96 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 96 times.
✗ Branch 10 not taken.
3808 , open_fds_(max_open_fds, FdWrapper(invalid_handle_, 0)) {
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1904 times.
1904 assert(max_open_fds > 0);
37
2/2
✓ Branch 0 taken 1181338 times.
✓ Branch 1 taken 1904 times.
1183242 for (unsigned i = 0; i < max_open_fds; ++i) {
38 1181338 fd_index_[i] = i;
39 1181338 open_fds_[i].index = i;
40 }
41 1904 }
42
43 /**
44 * Used to restore the state.
45 */
46 88 void AssignFrom(const FdTable<HandleT> &other) {
47 88 invalid_handle_ = other.invalid_handle_;
48 88 fd_pivot_ = other.fd_pivot_;
49 88 fd_index_.resize(other.fd_index_.size());
50
1/5
✗ Branch 2 not taken.
✓ Branch 3 taken 88 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
88 open_fds_.resize(other.open_fds_.size(), FdWrapper(invalid_handle_, 0));
51
2/2
✓ Branch 1 taken 11264 times.
✓ Branch 2 taken 88 times.
11352 for (unsigned i = 0; i < fd_index_.size(); ++i) {
52 11264 fd_index_[i] = other.fd_index_[i];
53 11264 open_fds_[i] = other.open_fds_[i];
54 }
55 88 }
56
57 /**
58 * Used to save the state.
59 */
60 88 FdTable<HandleT> *Clone() {
61 88 FdTable<HandleT> *result = new FdTable<HandleT>(open_fds_.size(),
62
1/2
✓ Branch 1 taken 88 times.
✗ Branch 2 not taken.
88 invalid_handle_);
63 88 result->fd_pivot_ = fd_pivot_;
64
2/2
✓ Branch 1 taken 11264 times.
✓ Branch 2 taken 88 times.
11352 for (unsigned i = 0; i < fd_index_.size(); ++i) {
65 11264 result->fd_index_[i] = fd_index_[i];
66 11264 result->open_fds_[i] = open_fds_[i];
67 }
68 88 return result;
69 }
70
71
72 /**
73 * Registers fd with a currently unused number. If the table is full,
74 * returns -ENFILE;
75 */
76 20883959 int OpenFd(const HandleT &handle) {
77
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 20484666 times.
✓ Branch 2 taken 399293 times.
20883959 if (handle == invalid_handle_)
78 return -EINVAL;
79
2/2
✓ Branch 1 taken 3383279 times.
✓ Branch 2 taken 17500680 times.
20883959 if (fd_pivot_ >= fd_index_.size())
80 3383279 return -ENFILE;
81
82 17500680 size_t const next_fd = fd_index_[fd_pivot_];
83
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17500680 times.
17500680 assert(next_fd < open_fds_.size());
84
2/3
✗ Branch 1 not taken.
✓ Branch 2 taken 17101510 times.
✓ Branch 3 taken 399170 times.
17500680 assert(open_fds_[next_fd].handle == invalid_handle_);
85
1/2
✓ Branch 2 taken 96 times.
✗ Branch 3 not taken.
17500680 open_fds_[next_fd] = FdWrapper(handle, fd_pivot_);
86 17500680 ++fd_pivot_;
87 17500680 return next_fd;
88 }
89
90 /**
91 * For invalid and unused numbers, the invalid handle is returned.
92 */
93 21320094 HandleT GetHandle(int fd) {
94
2/2
✓ Branch 1 taken 17893931 times.
✓ Branch 2 taken 3426163 times.
21320094 return IsValid(fd) ? open_fds_[fd].handle : invalid_handle_;
95 }
96
97
98 /**
99 * Releases fd back to the set of available numbers. Gracefully handles
100 * invalid handles (-EBADFD)
101 */
102 20914827 int CloseFd(int fd) {
103
2/2
✓ Branch 1 taken 3414644 times.
✓ Branch 2 taken 17500183 times.
20914827 if (!IsValid(fd))
104 3414644 return -EBADF;
105
106 17500183 unsigned const index = open_fds_[fd].index;
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17500183 times.
17500183 assert(index < fd_index_.size());
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17500183 times.
17500183 assert(fd_pivot_ <= fd_index_.size());
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17500183 times.
17500183 assert(fd_pivot_ > 0);
110 17500183 open_fds_[fd].handle = invalid_handle_;
111 17500183 --fd_pivot_;
112
2/2
✓ Branch 0 taken 9659949 times.
✓ Branch 1 taken 7840234 times.
17500183 if (index < fd_pivot_) {
113 9659949 unsigned const other = fd_index_[fd_pivot_];
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9659949 times.
9659949 assert(other < open_fds_.size());
115
2/3
✗ Branch 1 not taken.
✓ Branch 2 taken 9268706 times.
✓ Branch 3 taken 391243 times.
9659949 assert(open_fds_[other].handle != invalid_handle_);
116 9659949 open_fds_[other].index = index;
117 9659949 fd_index_[index] = other;
118 9659949 fd_index_[fd_pivot_] = fd;
119 }
120 17500183 return 0;
121 }
122
123 11264 unsigned GetMaxFds() const { return fd_index_.size(); }
124
125 private:
126 struct FdWrapper {
127 17502672 FdWrapper(HandleT h, unsigned i) : handle(h), index(i) { }
128
129 HandleT handle;
130 /**
131 * Back-pointer into fd_index_, which is needed when closing a file.
132 */
133 unsigned index;
134 };
135
136
137 42234921 inline bool IsValid(int fd) {
138
6/6
✓ Branch 0 taken 42234839 times.
✓ Branch 1 taken 82 times.
✓ Branch 3 taken 82 times.
✓ Branch 4 taken 42234757 times.
✓ Branch 5 taken 164 times.
✓ Branch 6 taken 42234757 times.
42234921 if ((fd < 0) || (static_cast<unsigned>(fd) >= open_fds_.size()))
139 164 return false;
140 42234757 return open_fds_[fd].handle != invalid_handle_;
141 }
142
143 /**
144 * An unused (available) file descriptor.
145 */
146 HandleT invalid_handle_;
147 /**
148 * The index of the first available file descriptor in fd_index_.
149 */
150 unsigned fd_pivot_;
151 /**
152 * Maps into open_fds_. Until fd_pivot_, file descriptors are used. As of
153 * fd_pivot_, points to free file descriptors. Used to acquire new file
154 * descriptors in constant time.
155 */
156 std::vector<unsigned> fd_index_;
157 /**
158 * The file descriptor number mapped to a user-defined file descriptor
159 * (struct). The fd integer passed to users of the file descriptor table
160 * points into this array.
161 */
162 std::vector<FdWrapper> open_fds_;
163 }; // class FdTable
164
165 #endif // CVMFS_FD_TABLE_H_
166