| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/bigvector.h |
| Date: | 2026-01-11 02:35:46 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 105 | 106 | 99.1% |
| Branches: | 31 | 44 | 70.5% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | * | ||
| 4 | * Dynamic array, allocate with mmap for large arrays. | ||
| 5 | */ | ||
| 6 | |||
| 7 | #ifndef CVMFS_BIGVECTOR_H_ | ||
| 8 | #define CVMFS_BIGVECTOR_H_ | ||
| 9 | |||
| 10 | #include <cassert> | ||
| 11 | #include <cstdlib> | ||
| 12 | |||
| 13 | #include "util/smalloc.h" | ||
| 14 | |||
| 15 | template<class Item> | ||
| 16 | class BigVector { | ||
| 17 | public: | ||
| 18 | 6899 | BigVector() { | |
| 19 | 6899 | buffer_ = Alloc(kNumInit); | |
| 20 | 6899 | size_ = 0; | |
| 21 | 6899 | shared_buffer_ = false; | |
| 22 | 6899 | } | |
| 23 | |||
| 24 | 1298969 | explicit BigVector(const size_t num_items) { | |
| 25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1298969 times.
|
1298969 | assert(num_items > 0); |
| 26 | 1298969 | buffer_ = Alloc(num_items); | |
| 27 | 1298969 | size_ = 0; | |
| 28 | 1298969 | shared_buffer_ = false; | |
| 29 | 1298969 | } | |
| 30 | |||
| 31 | 2501983 | BigVector(const BigVector<Item> &other) { CopyFrom(other); } | |
| 32 | |||
| 33 | 48 | BigVector<Item> &operator=(const BigVector<Item> &other) { | |
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if (&other == this) |
| 35 | ✗ | return *this; | |
| 36 | |||
| 37 |
1/2✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
|
48 | if (!shared_buffer_) |
| 38 | 48 | Dealloc(); | |
| 39 | 48 | CopyFrom(other); | |
| 40 | 48 | return *this; | |
| 41 | } | ||
| 42 | |||
| 43 | 7601235 | ~BigVector() { | |
| 44 |
1/2✓ Branch 0 taken 3800785 times.
✗ Branch 1 not taken.
|
7601235 | if (!shared_buffer_) |
| 45 | 7601317 | Dealloc(); | |
| 46 | 7603425 | } | |
| 47 | |||
| 48 | 1140582722 | Item At(const size_t index) const { | |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1140441858 times.
|
1140582722 | assert(index < size_); |
| 50 | 1140582722 | return buffer_[index]; | |
| 51 | } | ||
| 52 | |||
| 53 | 760010828 | const Item *AtPtr(const size_t index) const { | |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 760005414 times.
|
760010828 | assert(index < size_); |
| 55 | 760010828 | return &buffer_[index]; | |
| 56 | } | ||
| 57 | |||
| 58 | 1520362525 | void PushBack(const Item &item) { | |
| 59 |
2/2✓ Branch 0 taken 6221 times.
✓ Branch 1 taken 1520308828 times.
|
1520362525 | if (size_ == capacity_) |
| 60 | 6503 | DoubleCapacity(); | |
| 61 |
1/2✓ Branch 2 taken 237 times.
✗ Branch 3 not taken.
|
1520362525 | new (buffer_ + size_) Item(item); |
| 62 | 1520362525 | size_++; | |
| 63 | 1520362525 | } | |
| 64 | |||
| 65 | 85191 | void Replace(size_t index, const Item &item) { | |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 85191 times.
|
85191 | assert(index < size_); |
| 67 | 85191 | buffer_[index] = item; | |
| 68 | 85191 | } | |
| 69 | |||
| 70 | 135 | bool IsEmpty() const { return size_ == 0; } | |
| 71 | |||
| 72 | 205 | void Clear() { | |
| 73 | 205 | Dealloc(); | |
| 74 | 205 | buffer_ = Alloc(kNumInit); | |
| 75 | 205 | } | |
| 76 | |||
| 77 | 38 | void ShareBuffer(Item **duplicate, bool *large_alloc) { | |
| 78 | 38 | *duplicate = buffer_; | |
| 79 | 38 | *large_alloc = large_alloc_; | |
| 80 | 38 | shared_buffer_ = true; | |
| 81 | 38 | } | |
| 82 | |||
| 83 | 6503 | void DoubleCapacity() { | |
| 84 | 6503 | Item *old_buffer = buffer_; | |
| 85 | 6503 | const bool old_large_alloc = large_alloc_; | |
| 86 | |||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6221 times.
|
6503 | assert(capacity_ > 0); |
| 88 | 6503 | buffer_ = Alloc(capacity_ * 2); | |
| 89 |
2/2✓ Branch 0 taken 2550439727 times.
✓ Branch 1 taken 6221 times.
|
2550493606 | for (size_t i = 0; i < size_; ++i) |
| 90 |
0/2✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2550487103 | new (buffer_ + i) Item(old_buffer[i]); |
| 91 | |||
| 92 | 6503 | FreeBuffer(old_buffer, size_, old_large_alloc); | |
| 93 | 6503 | } | |
| 94 | |||
| 95 | 47267 | void ShrinkIfOversized() { | |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47267 times.
|
47267 | assert(!shared_buffer_); |
| 97 | |||
| 98 |
2/2✓ Branch 0 taken 990 times.
✓ Branch 1 taken 46277 times.
|
47267 | if (size_ <= kNumInit) |
| 99 | 990 | return; | |
| 100 |
2/2✓ Branch 0 taken 46051 times.
✓ Branch 1 taken 226 times.
|
46277 | if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_))) |
| 101 | 46051 | return; | |
| 102 | |||
| 103 | 226 | bool old_large_alloc = large_alloc_; | |
| 104 | 226 | Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_)); | |
| 105 |
2/2✓ Branch 0 taken 26172 times.
✓ Branch 1 taken 226 times.
|
26398 | for (size_t i = 0; i < size_; ++i) |
| 106 | 26172 | new (new_buffer + i) Item(buffer_[i]); | |
| 107 | 226 | FreeBuffer(buffer_, size_, old_large_alloc); | |
| 108 | 226 | buffer_ = new_buffer; | |
| 109 | } | ||
| 110 | |||
| 111 | // Careful! Only for externally modified buffer. | ||
| 112 | 47267 | void SetSize(const size_t new_size) { | |
| 113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 47267 times.
|
47267 | assert(new_size <= capacity_); |
| 114 | 47267 | size_ = new_size; | |
| 115 | 47267 | } | |
| 116 | |||
| 117 | 5432059 | size_t size() const { return size_; } | |
| 118 | 608 | size_t capacity() const { return capacity_; } | |
| 119 | |||
| 120 | private: | ||
| 121 | static const size_t kNumInit = 16; | ||
| 122 | static const size_t kMmapThreshold = 128 * 1024; | ||
| 123 | |||
| 124 | 7598799 | Item *Alloc(const size_t num_elements) { | |
| 125 | Item *result; | ||
| 126 | 7598799 | const size_t num_bytes = sizeof(Item) * num_elements; | |
| 127 |
2/2✓ Branch 0 taken 1681 times.
✓ Branch 1 taken 3801726 times.
|
7598799 | if (num_bytes >= kMmapThreshold) { |
| 128 | 1728 | result = static_cast<Item *>(smmap(num_bytes)); | |
| 129 | 1728 | large_alloc_ = true; | |
| 130 | } else { | ||
| 131 | 7597071 | result = static_cast<Item *>(smalloc(num_bytes)); | |
| 132 | 7597851 | large_alloc_ = false; | |
| 133 | } | ||
| 134 | 7599579 | capacity_ = num_elements; | |
| 135 | 7599579 | return result; | |
| 136 | } | ||
| 137 | |||
| 138 | 7601024 | void Dealloc() { | |
| 139 | 7601024 | FreeBuffer(buffer_, size_, large_alloc_); | |
| 140 | 7604456 | buffer_ = NULL; | |
| 141 | 7604456 | capacity_ = 0; | |
| 142 | 7604456 | size_ = 0; | |
| 143 | 7604456 | } | |
| 144 | |||
| 145 | 7608502 | void FreeBuffer(Item *buf, const size_t size, const bool large) { | |
| 146 |
2/2✓ Branch 0 taken 3690699868 times.
✓ Branch 1 taken 3807287 times.
|
3698492034 | for (size_t i = 0; i < size; ++i) |
| 147 | 3690883532 | buf[i].~Item(); | |
| 148 | |||
| 149 |
2/2✓ Branch 0 taken 3807262 times.
✓ Branch 1 taken 25 times.
|
7608502 | if (buf) { |
| 150 |
2/2✓ Branch 0 taken 1567 times.
✓ Branch 1 taken 3805695 times.
|
7608452 | if (large) { |
| 151 | 1614 | smunmap(buf); | |
| 152 | } else { | ||
| 153 | 7606838 | free(buf); | |
| 154 | } | ||
| 155 | } | ||
| 156 | 7608502 | } | |
| 157 | |||
| 158 | 2502071 | void CopyFrom(const BigVector<Item> &other) { | |
| 159 | 2502071 | buffer_ = Alloc(other.capacity_); | |
| 160 |
2/2✓ Branch 0 taken 760004115 times.
✓ Branch 1 taken 2502186 times.
|
762506301 | for (size_t i = 0; i < other.size_; ++i) { |
| 161 | 760004115 | new (buffer_ + i) Item(*other.AtPtr(i)); | |
| 162 | } | ||
| 163 | 2502186 | size_ = other.size_; | |
| 164 | 2502186 | shared_buffer_ = false; | |
| 165 | 2502186 | } | |
| 166 | |||
| 167 | Item *buffer_; | ||
| 168 | size_t size_; | ||
| 169 | size_t capacity_; | ||
| 170 | bool large_alloc_; | ||
| 171 | bool shared_buffer_; | ||
| 172 | }; | ||
| 173 | |||
| 174 | #endif // CVMFS_BIGVECTOR_H_ | ||
| 175 |