GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/shortstring.h
Date: 2025-05-11 02:35:43
Exec Total Coverage
Lines: 93 110 84.5%
Branches: 34 50 68.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 <cstring>
14 #include <string>
15
16 #include "util/atomic.h"
17
18 #ifdef CVMFS_NAMESPACE_GUARD
19 namespace CVMFS_NAMESPACE_GUARD {
20 #endif
21
22 const unsigned char kDefaultMaxName = 25;
23 const unsigned char kDefaultMaxLink = 25;
24 const unsigned char kDefaultMaxPath = 200;
25
26 template<unsigned char StackSize, char Type>
27 class ShortString {
28 public:
29 6016976 ShortString() : long_string_(NULL), length_(0) {
30 #ifdef DEBUGMSG
31 6016976 atomic_inc64(&num_instances_);
32 #endif
33 6016976 }
34 1925612 ShortString(const ShortString &other) : long_string_(NULL) {
35 #ifdef DEBUGMSG
36 3851224 atomic_inc64(&num_instances_);
37 #endif
38 3860620 Assign(other);
39 3853136 }
40 5469 ShortString(const char *chars, const unsigned length) : long_string_(NULL) {
41 #ifdef DEBUGMSG
42 5469 atomic_inc64(&num_instances_);
43 #endif
44 5533 Assign(chars, length);
45 5501 }
46 2172204 explicit ShortString(const std::string &std_string) : long_string_(NULL) {
47 #ifdef DEBUGMSG
48 2172204 atomic_inc64(&num_instances_);
49 #endif
50 2200312 Assign(std_string.data(), std_string.length());
51 2178256 }
52
53 1462302 ShortString & operator= (const ShortString & other) {
54
1/2
✓ Branch 0 taken 731540 times.
✗ Branch 1 not taken.
1462302 if (this != &other)
55 1463080 Assign(other);
56 1438594 return *this;
57 }
58
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5667378 times.
11334756 ~ShortString() { delete long_string_; }
60
61 7478520 void Assign(const char *chars, const unsigned length) {
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3739260 times.
7478520 delete long_string_;
63 7462538 long_string_ = NULL;
64 7462538 this->length_ = length;
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3731269 times.
7462538 if (length > StackSize) {
66 #ifdef DEBUGMSG
67 atomic_inc64(&num_overflows_);
68 #endif
69 long_string_ = new std::string(chars, length);
70 } else {
71
2/2
✓ Branch 0 taken 3719925 times.
✓ Branch 1 taken 11344 times.
7462538 if (length)
72 7439850 memcpy(stack_, chars, length);
73 }
74 7462538 }
75
76 5315884 void Assign(const ShortString &other) {
77 5315884 Assign(other.GetChars(), other.GetLength());
78 5292170 }
79
80 5692 void Append(const char *chars, const unsigned length) {
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5692 times.
5692 if (long_string_) {
82 long_string_->append(chars, length);
83 return;
84 }
85
86 5692 const unsigned new_length = this->length_ + length;
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5692 times.
5692 if (new_length > StackSize) {
88 #ifdef DEBUGMSG
89 atomic_inc64(&num_overflows_);
90 #endif
91 long_string_ = new std::string();
92 long_string_->reserve(new_length);
93 long_string_->assign(stack_, length_);
94 long_string_->append(chars, length);
95 return;
96 }
97
2/2
✓ Branch 0 taken 5652 times.
✓ Branch 1 taken 40 times.
5692 if (length > 0)
98 5652 memcpy(&stack_[this->length_], chars, length);
99 5692 this->length_ = new_length;
100 }
101
102 /**
103 * Truncates the current string to be of size smaller or equal to current size
104 *
105 * Note: Can lead to a heap allocated string that is shorter than
106 * the reserved stack space.
107 */
108 54 void Truncate(unsigned new_length) {
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 54 times.
54 assert(new_length <= this->GetLength());
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (long_string_) {
111 long_string_->erase(new_length);
112 return;
113 }
114 54 this->length_ = new_length;
115 }
116
117 void Clear() {
118 delete long_string_;
119 long_string_ = NULL;
120 length_ = 0;
121 }
122
123 7545402 const char *GetChars() const {
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3772701 times.
7545402 if (long_string_) {
125 return long_string_->data();
126 } else {
127 7545402 return stack_;
128 }
129 }
130
131 7575462 unsigned GetLength() const {
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3787731 times.
7575462 if (long_string_)
133 return long_string_->length();
134 7575462 return length_;
135 }
136
137 1280 bool IsEmpty() const {
138 1280 return GetLength() == 0;
139 }
140
141 1112134 std::string ToString() const {
142
1/2
✓ Branch 4 taken 1111822 times.
✗ Branch 5 not taken.
1112134 return std::string(this->GetChars(), this->GetLength());
143 }
144
145 3930 const char *c_str() const {
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2611 times.
3930 if (long_string_)
147 return long_string_->c_str();
148
149 3930 char *c = const_cast<char *>(stack_) + length_;
150 3930 *c = '\0';
151 3930 return stack_;
152 }
153
154 1137 bool operator ==(const ShortString &other) const {
155 1137 const unsigned this_length = this->GetLength();
156 1137 const unsigned other_length = other.GetLength();
157
2/2
✓ Branch 0 taken 724 times.
✓ Branch 1 taken 352 times.
1137 if (this_length != other_length)
158 731 return false;
159
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 168 times.
406 if (this_length == 0)
160 222 return true;
161
162 184 return memcmp(this->GetChars(), other.GetChars(), this_length) == 0;
163 }
164
165 737 bool operator !=(const ShortString &other) const {
166 737 return !(*this == other);
167 }
168
169 1367 bool operator <(const ShortString &other) const {
170 1367 const unsigned this_length = this->GetLength();
171 1367 const unsigned other_length = other.GetLength();
172
173
2/2
✓ Branch 0 taken 354 times.
✓ Branch 1 taken 1013 times.
1367 if (this_length < other_length)
174 354 return true;
175
2/2
✓ Branch 0 taken 238 times.
✓ Branch 1 taken 775 times.
1013 if (this_length > other_length)
176 238 return false;
177
178 775 const char *this_chars = this->GetChars();
179 775 const char *other_chars = other.GetChars();
180
2/2
✓ Branch 0 taken 7875 times.
✓ Branch 1 taken 676 times.
8551 for (unsigned i = 0; i < this_length; ++i) {
181
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 7798 times.
7875 if (this_chars[i] < other_chars[i])
182 77 return true;
183
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 7776 times.
7798 if (this_chars[i] > other_chars[i])
184 22 return false;
185 }
186 676 return false;
187 }
188
189 1207 bool StartsWith(const ShortString &other) const {
190 1207 const unsigned this_length = this->GetLength();
191 1207 const unsigned other_length = other.GetLength();
192
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 1137 times.
1207 if (this_length < other_length)
193 70 return false;
194
195 1137 return memcmp(this->GetChars(), other.GetChars(), other_length) == 0;
196 }
197
198 514 ShortString Suffix(const unsigned start_at) const {
199 514 const unsigned length = this->GetLength();
200
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 510 times.
514 if (start_at >= length)
201 4 return ShortString("", 0);
202
203 510 return ShortString(this->GetChars() + start_at, length-start_at);
204 }
205
206 static uint64_t num_instances() { return atomic_read64(&num_instances_); }
207 static uint64_t num_overflows() { return atomic_read64(&num_overflows_); }
208
209 private:
210 std::string *long_string_;
211 char stack_[StackSize+1]; // +1 to add a final '\0' if necessary
212 unsigned char length_;
213 static atomic_int64 num_overflows_;
214 static atomic_int64 num_instances_;
215 }; // class ShortString
216
217 typedef ShortString<kDefaultMaxPath, 0> PathString;
218 typedef ShortString<kDefaultMaxName, 1> NameString;
219 typedef ShortString<kDefaultMaxLink, 2> LinkString;
220
221 template<unsigned char StackSize, char Type>
222 atomic_int64 ShortString<StackSize, Type>::num_overflows_ = 0;
223 template<unsigned char StackSize, char Type>
224 atomic_int64 ShortString<StackSize, Type>::num_instances_ = 0;
225
226 // See posix.cc for the std::string counterparts
227 PathString GetParentPath(const PathString &path);
228 NameString GetFileName(const PathString &path);
229
230
231 #ifdef CVMFS_NAMESPACE_GUARD
232 } // namespace CVMFS_NAMESPACE_GUARD
233 #endif
234
235 #endif // CVMFS_SHORTSTRING_H_
236