Directory: | cvmfs/ |
---|---|
File: | cvmfs/shortstring.h |
Date: | 2024-04-28 02:33:07 |
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 | 6138552 | ShortString() : long_string_(NULL), length_(0) { | |
30 | #ifdef DEBUGMSG | ||
31 | 6138552 | atomic_inc64(&num_instances_); | |
32 | #endif | ||
33 | 6138552 | } | |
34 | 1922582 | ShortString(const ShortString &other) : long_string_(NULL) { | |
35 | #ifdef DEBUGMSG | ||
36 | 3845164 | atomic_inc64(&num_instances_); | |
37 | #endif | ||
38 | 3857494 | Assign(other); | |
39 | 3848282 | } | |
40 | 5399 | ShortString(const char *chars, const unsigned length) : long_string_(NULL) { | |
41 | #ifdef DEBUGMSG | ||
42 | 5399 | atomic_inc64(&num_instances_); | |
43 | #endif | ||
44 | 5428 | Assign(chars, length); | |
45 | 5411 | } | |
46 | 2175112 | explicit ShortString(const std::string &std_string) : long_string_(NULL) { | |
47 | #ifdef DEBUGMSG | ||
48 | 2175112 | atomic_inc64(&num_instances_); | |
49 | #endif | ||
50 | 2201016 | Assign(std_string.data(), std_string.length()); | |
51 | 2180516 | } | |
52 | |||
53 | 1451872 | ShortString & operator= (const ShortString & other) { | |
54 |
1/2✓ Branch 0 taken 726607 times.
✗ Branch 1 not taken.
|
1451872 | if (this != &other) |
55 | 1453214 | Assign(other); | |
56 | 1436816 | return *this; | |
57 | } | ||
58 | |||
59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5722440 times.
|
11444880 | ~ShortString() { delete long_string_; } |
60 | |||
61 | 7469490 | void Assign(const char *chars, const unsigned length) { | |
62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3734745 times.
|
7469490 | delete long_string_; |
63 | 7456228 | long_string_ = NULL; | |
64 | 7456228 | this->length_ = length; | |
65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3728114 times.
|
7456228 | 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 3717671 times.
✓ Branch 1 taken 10443 times.
|
7456228 | if (length) |
72 | 7435342 | memcpy(stack_, chars, length); | |
73 | } | ||
74 | 7456228 | } | |
75 | |||
76 | 5302492 | void Assign(const ShortString &other) { | |
77 | 5302492 | Assign(other.GetChars(), other.GetLength()); | |
78 | 5285126 | } | |
79 | |||
80 | 4817 | void Append(const char *chars, const unsigned length) { | |
81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4817 times.
|
4817 | if (long_string_) { |
82 | ✗ | long_string_->append(chars, length); | |
83 | ✗ | return; | |
84 | } | ||
85 | |||
86 | 4817 | const unsigned new_length = this->length_ + length; | |
87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4817 times.
|
4817 | 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 4777 times.
✓ Branch 1 taken 40 times.
|
4817 | if (length > 0) |
98 | 4777 | memcpy(&stack_[this->length_], chars, length); | |
99 | 4817 | 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 | 7528688 | const char *GetChars() const { | |
124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3764344 times.
|
7528688 | if (long_string_) { |
125 | ✗ | return long_string_->data(); | |
126 | } else { | ||
127 | 7528688 | return stack_; | |
128 | } | ||
129 | } | ||
130 | |||
131 | 7555452 | unsigned GetLength() const { | |
132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3777726 times.
|
7555452 | if (long_string_) |
133 | ✗ | return long_string_->length(); | |
134 | 7555452 | return length_; | |
135 | } | ||
136 | |||
137 | 1276 | bool IsEmpty() const { | |
138 | 1276 | return GetLength() == 0; | |
139 | } | ||
140 | |||
141 | 1112035 | std::string ToString() const { | |
142 |
1/2✓ Branch 4 taken 1111744 times.
✗ Branch 5 not taken.
|
1112035 | return std::string(this->GetChars(), this->GetLength()); |
143 | } | ||
144 | |||
145 | 3792 | const char *c_str() const { | |
146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2475 times.
|
3792 | if (long_string_) |
147 | ✗ | return long_string_->c_str(); | |
148 | |||
149 | 3792 | char *c = const_cast<char *>(stack_) + length_; | |
150 | 3792 | *c = '\0'; | |
151 | 3792 | return stack_; | |
152 | } | ||
153 | |||
154 | 1027 | bool operator ==(const ShortString &other) const { | |
155 | 1027 | const unsigned this_length = this->GetLength(); | |
156 | 1027 | const unsigned other_length = other.GetLength(); | |
157 |
2/2✓ Branch 0 taken 641 times.
✓ Branch 1 taken 325 times.
|
1027 | if (this_length != other_length) |
158 | 648 | return false; | |
159 |
2/2✓ Branch 0 taken 176 times.
✓ Branch 1 taken 149 times.
|
379 | if (this_length == 0) |
160 | 214 | return true; | |
161 | |||
162 | 165 | return memcmp(this->GetChars(), other.GetChars(), this_length) == 0; | |
163 | } | ||
164 | |||
165 | 650 | bool operator !=(const ShortString &other) const { | |
166 | 650 | return !(*this == other); | |
167 | } | ||
168 | |||
169 | 1187 | bool operator <(const ShortString &other) const { | |
170 | 1187 | const unsigned this_length = this->GetLength(); | |
171 | 1187 | const unsigned other_length = other.GetLength(); | |
172 | |||
173 |
2/2✓ Branch 0 taken 315 times.
✓ Branch 1 taken 872 times.
|
1187 | if (this_length < other_length) |
174 | 315 | return true; | |
175 |
2/2✓ Branch 0 taken 202 times.
✓ Branch 1 taken 670 times.
|
872 | if (this_length > other_length) |
176 | 202 | return false; | |
177 | |||
178 | 670 | const char *this_chars = this->GetChars(); | |
179 | 670 | const char *other_chars = other.GetChars(); | |
180 |
2/2✓ Branch 0 taken 6300 times.
✓ Branch 1 taken 600 times.
|
6900 | for (unsigned i = 0; i < this_length; ++i) { |
181 |
2/2✓ Branch 0 taken 58 times.
✓ Branch 1 taken 6242 times.
|
6300 | if (this_chars[i] < other_chars[i]) |
182 | 58 | return true; | |
183 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6230 times.
|
6242 | if (this_chars[i] > other_chars[i]) |
184 | 12 | return false; | |
185 | } | ||
186 | 600 | return false; | |
187 | } | ||
188 | |||
189 | 1061 | bool StartsWith(const ShortString &other) const { | |
190 | 1061 | const unsigned this_length = this->GetLength(); | |
191 | 1061 | const unsigned other_length = other.GetLength(); | |
192 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 997 times.
|
1061 | if (this_length < other_length) |
193 | 64 | return false; | |
194 | |||
195 | 997 | return memcmp(this->GetChars(), other.GetChars(), other_length) == 0; | |
196 | } | ||
197 | |||
198 | 442 | ShortString Suffix(const unsigned start_at) const { | |
199 | 442 | const unsigned length = this->GetLength(); | |
200 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 438 times.
|
442 | if (start_at >= length) |
201 | 4 | return ShortString("", 0); | |
202 | |||
203 | 438 | 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 |