| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/shortstring.h |
| Date: | 2026-01-11 02:35:46 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 91 | 108 | 84.3% |
| 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 | 139393410 | ShortString() : long_string_(NULL), length_(0) { | |
| 30 | #ifdef DEBUGMSG | ||
| 31 | 139393410 | atomic_inc64(&num_instances_); | |
| 32 | #endif | ||
| 33 | 139393410 | } | |
| 34 | 72728944 | ShortString(const ShortString &other) : long_string_(NULL) { | |
| 35 | #ifdef DEBUGMSG | ||
| 36 | 145457888 | atomic_inc64(&num_instances_); | |
| 37 | #endif | ||
| 38 | 145919968 | Assign(other); | |
| 39 | 145549392 | } | |
| 40 | 204054 | ShortString(const char *chars, const unsigned length) : long_string_(NULL) { | |
| 41 | #ifdef DEBUGMSG | ||
| 42 | 204054 | atomic_inc64(&num_instances_); | |
| 43 | #endif | ||
| 44 | 205270 | Assign(chars, length); | |
| 45 | 204472 | } | |
| 46 | 82584802 | explicit ShortString(const std::string &std_string) : long_string_(NULL) { | |
| 47 | #ifdef DEBUGMSG | ||
| 48 | 82584802 | atomic_inc64(&num_instances_); | |
| 49 | #endif | ||
| 50 | 83514130 | Assign(std_string.data(), std_string.length()); | |
| 51 | 82613910 | } | |
| 52 | |||
| 53 | 55400886 | ShortString &operator=(const ShortString &other) { | |
| 54 |
1/2✓ Branch 0 taken 27715073 times.
✗ Branch 1 not taken.
|
55400886 | if (this != &other) |
| 55 | 55430146 | Assign(other); | |
| 56 | 54960998 | return *this; | |
| 57 | } | ||
| 58 | |||
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 170420560 times.
|
340841120 | ~ShortString() { delete long_string_; } |
| 60 | |||
| 61 | 283477516 | void Assign(const char *chars, const unsigned length) { | |
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 141738758 times.
|
283477516 | delete long_string_; |
| 63 | 283574872 | long_string_ = NULL; | |
| 64 | 283574872 | this->length_ = length; | |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 141787436 times.
|
283574872 | 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 141231468 times.
✓ Branch 1 taken 555968 times.
|
283574872 | if (length) |
| 72 | 282462936 | memcpy(stack_, chars, length); | |
| 73 | } | ||
| 74 | 283574872 | } | |
| 75 | |||
| 76 | 201099064 | void Assign(const ShortString &other) { | |
| 77 | 201099064 | Assign(other.GetChars(), other.GetLength()); | |
| 78 | 200544340 | } | |
| 79 | |||
| 80 | 61656 | void Append(const char *chars, const unsigned length) { | |
| 81 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61656 times.
|
61656 | if (long_string_) { |
| 82 | ✗ | long_string_->append(chars, length); | |
| 83 | ✗ | return; | |
| 84 | } | ||
| 85 | |||
| 86 | 61656 | const unsigned new_length = this->length_ + length; | |
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 61656 times.
|
61656 | 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 59928 times.
✓ Branch 1 taken 1728 times.
|
61656 | if (length > 0) |
| 98 | 59928 | memcpy(&stack_[this->length_], chars, length); | |
| 99 | 61656 | 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 | 2322 | void Truncate(unsigned new_length) { | |
| 109 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2322 times.
|
2322 | assert(new_length <= this->GetLength()); |
| 110 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2322 times.
|
2322 | if (long_string_) { |
| 111 | ✗ | long_string_->erase(new_length); | |
| 112 | ✗ | return; | |
| 113 | } | ||
| 114 | 2322 | this->length_ = new_length; | |
| 115 | } | ||
| 116 | |||
| 117 | void Clear() { | ||
| 118 | delete long_string_; | ||
| 119 | long_string_ = NULL; | ||
| 120 | length_ = 0; | ||
| 121 | } | ||
| 122 | |||
| 123 | 285499422 | const char *GetChars() const { | |
| 124 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 142749711 times.
|
285499422 | if (long_string_) { |
| 125 | ✗ | return long_string_->data(); | |
| 126 | } else { | ||
| 127 | 285499422 | return stack_; | |
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | 286125090 | unsigned GetLength() const { | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 143062545 times.
|
286125090 | if (long_string_) |
| 133 | ✗ | return long_string_->length(); | |
| 134 | 286125090 | return length_; | |
| 135 | } | ||
| 136 | |||
| 137 | 49639 | bool IsEmpty() const { return GetLength() == 0; } | |
| 138 | |||
| 139 | 42145734 | std::string ToString() const { | |
| 140 |
1/2✓ Branch 4 taken 42140461 times.
✗ Branch 5 not taken.
|
42145734 | return std::string(this->GetChars(), this->GetLength()); |
| 141 | } | ||
| 142 | |||
| 143 | 133460 | const char *c_str() const { | |
| 144 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 77846 times.
|
133460 | if (long_string_) |
| 145 | ✗ | return long_string_->c_str(); | |
| 146 | |||
| 147 | 133460 | char *c = const_cast<char *>(stack_) + length_; | |
| 148 | 133460 | *c = '\0'; | |
| 149 | 133460 | return stack_; | |
| 150 | } | ||
| 151 | |||
| 152 | 23853 | bool operator==(const ShortString &other) const { | |
| 153 | 23853 | const unsigned this_length = this->GetLength(); | |
| 154 | 23853 | const unsigned other_length = other.GetLength(); | |
| 155 |
2/2✓ Branch 0 taken 11874 times.
✓ Branch 1 taken 9405 times.
|
23853 | if (this_length != other_length) |
| 156 | 12187 | return false; | |
| 157 |
2/2✓ Branch 0 taken 5390 times.
✓ Branch 1 taken 4015 times.
|
11666 | if (this_length == 0) |
| 158 | 6999 | return true; | |
| 159 | |||
| 160 | 4667 | return memcmp(this->GetChars(), other.GetChars(), this_length) == 0; | |
| 161 | } | ||
| 162 | |||
| 163 | 13437 | bool operator!=(const ShortString &other) const { return !(*this == other); } | |
| 164 | |||
| 165 | 20097 | bool operator<(const ShortString &other) const { | |
| 166 | 20097 | const unsigned this_length = this->GetLength(); | |
| 167 | 20097 | const unsigned other_length = other.GetLength(); | |
| 168 | |||
| 169 |
2/2✓ Branch 0 taken 5763 times.
✓ Branch 1 taken 14334 times.
|
20097 | if (this_length < other_length) |
| 170 | 5763 | return true; | |
| 171 |
2/2✓ Branch 0 taken 2719 times.
✓ Branch 1 taken 11615 times.
|
14334 | if (this_length > other_length) |
| 172 | 2719 | return false; | |
| 173 | |||
| 174 | 11615 | const char *this_chars = this->GetChars(); | |
| 175 | 11615 | const char *other_chars = other.GetChars(); | |
| 176 |
2/2✓ Branch 0 taken 93449 times.
✓ Branch 1 taken 10336 times.
|
103785 | for (unsigned i = 0; i < this_length; ++i) { |
| 177 |
2/2✓ Branch 0 taken 970 times.
✓ Branch 1 taken 92479 times.
|
93449 | if (this_chars[i] < other_chars[i]) |
| 178 | 970 | return true; | |
| 179 |
2/2✓ Branch 0 taken 309 times.
✓ Branch 1 taken 92170 times.
|
92479 | if (this_chars[i] > other_chars[i]) |
| 180 | 309 | return false; | |
| 181 | } | ||
| 182 | 10336 | return false; | |
| 183 | } | ||
| 184 | |||
| 185 | 17766 | bool StartsWith(const ShortString &other) const { | |
| 186 | 17766 | const unsigned this_length = this->GetLength(); | |
| 187 | 17766 | const unsigned other_length = other.GetLength(); | |
| 188 |
2/2✓ Branch 0 taken 1035 times.
✓ Branch 1 taken 16731 times.
|
17766 | if (this_length < other_length) |
| 189 | 1035 | return false; | |
| 190 | |||
| 191 | 16731 | return memcmp(this->GetChars(), other.GetChars(), other_length) == 0; | |
| 192 | } | ||
| 193 | |||
| 194 | 6081 | ShortString Suffix(const unsigned start_at) const { | |
| 195 | 6081 | const unsigned length = this->GetLength(); | |
| 196 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 5901 times.
|
6081 | if (start_at >= length) |
| 197 | 180 | return ShortString("", 0); | |
| 198 | |||
| 199 | 5901 | return ShortString(this->GetChars() + start_at, length - start_at); | |
| 200 | } | ||
| 201 | |||
| 202 | ✗ | static uint64_t num_instances() { return atomic_read64(&num_instances_); } | |
| 203 | ✗ | static uint64_t num_overflows() { return atomic_read64(&num_overflows_); } | |
| 204 | |||
| 205 | private: | ||
| 206 | std::string *long_string_; | ||
| 207 | char stack_[StackSize + 1]; // +1 to add a final '\0' if necessary | ||
| 208 | unsigned char length_; | ||
| 209 | static atomic_int64 num_overflows_; | ||
| 210 | static atomic_int64 num_instances_; | ||
| 211 | }; // class ShortString | ||
| 212 | |||
| 213 | typedef ShortString<kDefaultMaxPath, 0> PathString; | ||
| 214 | typedef ShortString<kDefaultMaxName, 1> NameString; | ||
| 215 | typedef ShortString<kDefaultMaxLink, 2> LinkString; | ||
| 216 | |||
| 217 | template<unsigned char StackSize, char Type> | ||
| 218 | atomic_int64 ShortString<StackSize, Type>::num_overflows_ = 0; | ||
| 219 | template<unsigned char StackSize, char Type> | ||
| 220 | atomic_int64 ShortString<StackSize, Type>::num_instances_ = 0; | ||
| 221 | |||
| 222 | // See posix.cc for the std::string counterparts | ||
| 223 | PathString GetParentPath(const PathString &path); | ||
| 224 | NameString GetFileName(const PathString &path); | ||
| 225 | |||
| 226 | bool IsSubPath(const PathString &parent, const PathString &path); | ||
| 227 | |||
| 228 | |||
| 229 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 230 | } // namespace CVMFS_NAMESPACE_GUARD | ||
| 231 | #endif | ||
| 232 | |||
| 233 | #endif // CVMFS_SHORTSTRING_H_ | ||
| 234 |