GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/shortstring.h
Date: 2026-10-25 02:40:14
Exec Total Coverage
Lines: 91 103 88.3%
Branches: 40 50 80.0%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Implements a string class that stores short strings on the stack and
5 * malloc's a std::string on the heap on overflow. Used for file names and
6 * path names that are usually small.
7 */
8
9 #ifndef CVMFS_SHORTSTRING_H_
10 #define CVMFS_SHORTSTRING_H_
11
12 #include <algorithm>
13 #include <atomic>
14 #include <cstring>
15 #include <string>
16
17 #ifdef CVMFS_NAMESPACE_GUARD
18 namespace CVMFS_NAMESPACE_GUARD {
19 #endif
20
21 const unsigned char kDefaultMaxName = 25;
22 const unsigned char kDefaultMaxLink = 25;
23 const unsigned char kDefaultMaxPath = 200;
24
25 template<unsigned char StackSize, char Type>
26 class ShortString {
27 public:
28 211588950 ShortString() : long_string_(NULL), length_(0) {
29 #ifdef DEBUGMSG
30 num_instances_.fetch_add(1);
31 #endif
32 211588950 }
33 17663237 ShortString(const ShortString &other) : long_string_(NULL) {
34 #ifdef DEBUGMSG
35 num_instances_.fetch_add(1);
36 #endif
37 35326474 Assign(other);
38 35350234 }
39 52323 ShortString(const char *chars, const unsigned length) : long_string_(NULL) {
40 #ifdef DEBUGMSG
41 num_instances_.fetch_add(1);
42 #endif
43 52323 Assign(chars, length);
44 52371 }
45 15642670 explicit ShortString(const std::string &std_string) : long_string_(NULL) {
46 #ifdef DEBUGMSG
47 num_instances_.fetch_add(1);
48 #endif
49 15642670 Assign(std_string.data(), std_string.length());
50 15662824 }
51
52 8986880 ShortString &operator=(const ShortString &other) {
53
1/2
✓ Branch 0 taken 4495507 times.
✗ Branch 1 not taken.
8986880 if (this != &other)
54 8991014 Assign(other);
55 8952080 return *this;
56 }
57
58
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 128038889 times.
256077814 ~ShortString() { delete long_string_; }
59
60 60108456 void Assign(const char *chars, const unsigned length) {
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30054228 times.
60108456 delete long_string_;
62 60078870 long_string_ = NULL;
63 60078870 this->length_ = length;
64
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 30039417 times.
60078870 if (length > StackSize) {
65 #ifdef DEBUGMSG
66 num_overflows_.fetch_add(1);
67 #endif
68
2/4
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 18 times.
✗ Branch 6 not taken.
36 long_string_ = new std::string(chars, length);
69 } else {
70
2/2
✓ Branch 0 taken 29835353 times.
✓ Branch 1 taken 204064 times.
60078834 if (length)
71 59670706 memcpy(stack_, chars, length);
72 }
73 60078870 }
74
75 44384022 void Assign(const ShortString &other) {
76 44384022 Assign(other.GetChars(), other.GetLength());
77 44306994 }
78
79 104543 void Append(const char *chars, const unsigned length) {
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104543 times.
104543 if (long_string_) {
81 ✗ long_string_->append(chars, length);
82 ✗ return;
83 }
84
85 104543 const unsigned new_length = this->length_ + length;
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104543 times.
104543 if (new_length > StackSize) {
87 #ifdef DEBUGMSG
88 num_overflows_.fetch_add(1);
89 #endif
90 ✗ long_string_ = new std::string();
91 ✗ long_string_->reserve(new_length);
92 ✗ long_string_->assign(stack_, length_);
93 ✗ long_string_->append(chars, length);
94 ✗ return;
95 }
96
2/2
✓ Branch 0 taken 101739 times.
✓ Branch 1 taken 2804 times.
104543 if (length > 0)
97 101739 memcpy(&stack_[this->length_], chars, length);
98 104543 this->length_ = new_length;
99 }
100
101 /**
102 * Truncates the current string to be of size smaller or equal to current size
103 *
104 * Note: Can lead to a heap allocated string that is shorter than
105 * the reserved stack space.
106 */
107 3744 void Truncate(unsigned new_length) {
108
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3744 times.
3744 assert(new_length <= this->GetLength());
109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3744 times.
3744 if (long_string_) {
110 ✗ long_string_->erase(new_length);
111 ✗ return;
112 }
113 3744 this->length_ = new_length;
114 }
115
116 void Clear() {
117 delete long_string_;
118 long_string_ = NULL;
119 length_ = 0;
120 }
121
122 60519762 const char *GetChars() const {
123
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 30259854 times.
60519762 if (long_string_) {
124 54 return long_string_->data();
125 } else {
126 60519708 return stack_;
127 }
128 }
129
130 60945128 unsigned GetLength() const {
131
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 30472537 times.
60945128 if (long_string_)
132 54 return long_string_->length();
133 60945074 return length_;
134 }
135
136 16840 bool IsEmpty() const { return GetLength() == 0; }
137
138 7868751 std::string ToString() const {
139
1/2
✓ Branch 4 taken 7864958 times.
✗ Branch 5 not taken.
7868751 return std::string(this->GetChars(), this->GetLength());
140 }
141
142 51923 const char *c_str() const {
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39467 times.
51923 if (long_string_)
144 ✗ return long_string_->c_str();
145
146 51923 char *c = const_cast<char *>(stack_) + length_;
147 51923 *c = '\0';
148 51923 return stack_;
149 }
150
151 27342 bool operator==(const ShortString &other) const {
152 27342 const unsigned this_length = this->GetLength();
153 27342 const unsigned other_length = other.GetLength();
154
2/2
✓ Branch 0 taken 11739 times.
✓ Branch 1 taken 12104 times.
27342 if (this_length != other_length)
155 11991 return false;
156
2/2
✓ Branch 0 taken 7856 times.
✓ Branch 1 taken 4248 times.
15351 if (this_length == 0)
157 10463 return true;
158
159 4888 return memcmp(this->GetChars(), other.GetChars(), this_length) == 0;
160 }
161
162 16285 bool operator!=(const ShortString &other) const { return !(*this == other); }
163
164 18988 bool operator<(const ShortString &other) const {
165 18988 const unsigned this_length = this->GetLength();
166 18988 const unsigned other_length = other.GetLength();
167
168
2/2
✓ Branch 0 taken 3223 times.
✓ Branch 1 taken 15765 times.
18988 if (this_length < other_length)
169 3223 return true;
170
2/2
✓ Branch 0 taken 2595 times.
✓ Branch 1 taken 13170 times.
15765 if (this_length > other_length)
171 2595 return false;
172
173 13170 const char *this_chars = this->GetChars();
174 13170 const char *other_chars = other.GetChars();
175
2/2
✓ Branch 0 taken 96376 times.
✓ Branch 1 taken 11364 times.
107740 for (unsigned i = 0; i < this_length; ++i) {
176
2/2
✓ Branch 0 taken 1372 times.
✓ Branch 1 taken 95004 times.
96376 if (this_chars[i] < other_chars[i])
177 1372 return true;
178
2/2
✓ Branch 0 taken 434 times.
✓ Branch 1 taken 94570 times.
95004 if (this_chars[i] > other_chars[i])
179 434 return false;
180 }
181 11364 return false;
182 }
183
184 22284 bool StartsWith(const ShortString &other) const {
185 22284 const unsigned this_length = this->GetLength();
186 22284 const unsigned other_length = other.GetLength();
187
2/2
✓ Branch 0 taken 1503 times.
✓ Branch 1 taken 20781 times.
22284 if (this_length < other_length)
188 1503 return false;
189
190 20781 return memcmp(this->GetChars(), other.GetChars(), other_length) == 0;
191 }
192
193 9526 ShortString Suffix(const unsigned start_at) const {
194 9526 const unsigned length = this->GetLength();
195
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 9386 times.
9526 if (start_at >= length)
196 140 return ShortString("", 0);
197
198 9386 return ShortString(this->GetChars() + start_at, length - start_at);
199 }
200
201 ✗ static uint64_t num_instances() { return num_instances_.load(); }
202 ✗ static uint64_t num_overflows() { return num_overflows_.load(); }
203
204 72 operator bool() const { return not IsEmpty(); }
205
206 private:
207 std::string *long_string_;
208 char stack_[StackSize + 1]; // +1 to add a final '\0' if necessary
209 unsigned char length_;
210 static std::atomic<int64_t> num_overflows_;
211 static std::atomic<int64_t> num_instances_;
212 }; // class ShortString
213
214 typedef ShortString<kDefaultMaxPath, 0> PathString;
215 typedef ShortString<kDefaultMaxName, 1> NameString;
216 typedef ShortString<kDefaultMaxLink, 2> LinkString;
217
218 template<unsigned char StackSize, char Type>
219 std::atomic<int64_t> ShortString<StackSize, Type>::num_overflows_(0);
220 template<unsigned char StackSize, char Type>
221 std::atomic<int64_t> ShortString<StackSize, Type>::num_instances_(0);
222
223 // See posix.cc for the std::string counterparts
224 PathString GetParentPath(const PathString &path);
225 NameString GetFileName(const PathString &path);
226
227 bool IsSubPath(const PathString &parent, const PathString &path);
228
229
230 #ifdef CVMFS_NAMESPACE_GUARD
231 } // namespace CVMFS_NAMESPACE_GUARD
232 #endif
233
234 #endif // CVMFS_SHORTSTRING_H_
235
236