| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/bigvector.h |
| Date: | 2026-06-28 02:36:10 |
| 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 | 5455 | BigVector() { | |
| 19 | 5455 | buffer_ = Alloc(kNumInit); | |
| 20 | 5455 | size_ = 0; | |
| 21 | 5455 | shared_buffer_ = false; | |
| 22 | 5455 | } | |
| 23 | |||
| 24 | 320826 | explicit BigVector(const size_t num_items) { | |
| 25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 320826 times.
|
320826 | assert(num_items > 0); |
| 26 | 320826 | buffer_ = Alloc(num_items); | |
| 27 | 320826 | size_ = 0; | |
| 28 | 320826 | shared_buffer_ = false; | |
| 29 | 320826 | } | |
| 30 | |||
| 31 | 505042 | BigVector(const BigVector<Item> &other) { CopyFrom(other); } | |
| 32 | |||
| 33 | 35 | BigVector<Item> &operator=(const BigVector<Item> &other) { | |
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
|
35 | if (&other == this) |
| 35 | ✗ | return *this; | |
| 36 | |||
| 37 |
1/2✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
|
35 | if (!shared_buffer_) |
| 38 | 35 | Dealloc(); | |
| 39 | 35 | CopyFrom(other); | |
| 40 | 35 | return *this; | |
| 41 | } | ||
| 42 | |||
| 43 | 1655556 | ~BigVector() { | |
| 44 |
2/2✓ Branch 0 taken 827764 times.
✓ Branch 1 taken 67 times.
|
1655556 | if (!shared_buffer_) |
| 45 | 1655455 | Dealloc(); | |
| 46 | 1655774 | } | |
| 47 | |||
| 48 | 990382034 | Item At(const size_t index) const { | |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 990253166 times.
|
990382034 | assert(index < size_); |
| 50 | 990382034 | return buffer_[index]; | |
| 51 | } | ||
| 52 | |||
| 53 | 660004736 | const Item *AtPtr(const size_t index) const { | |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 660002368 times.
|
660004736 | assert(index < size_); |
| 55 | 660004736 | return &buffer_[index]; | |
| 56 | } | ||
| 57 | |||
| 58 | 1320197035 | void PushBack(const Item &item) { | |
| 59 |
2/2✓ Branch 0 taken 4038 times.
✓ Branch 1 taken 1320149449 times.
|
1320197035 | if (size_ == capacity_) |
| 60 | 4296 | DoubleCapacity(); | |
| 61 |
1/2✓ Branch 2 taken 337 times.
✗ Branch 3 not taken.
|
1320197035 | new (buffer_ + size_) Item(item); |
| 62 | 1320197035 | size_++; | |
| 63 | 1320197035 | } | |
| 64 | |||
| 65 | 76169 | void Replace(size_t index, const Item &item) { | |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 76169 times.
|
76169 | assert(index < size_); |
| 67 | 76169 | buffer_[index] = item; | |
| 68 | 76169 | } | |
| 69 | |||
| 70 | 33 | bool IsEmpty() const { return size_ == 0; } | |
| 71 | |||
| 72 | 90 | void Clear() { | |
| 73 | 90 | Dealloc(); | |
| 74 | 90 | buffer_ = Alloc(kNumInit); | |
| 75 | 90 | } | |
| 76 | |||
| 77 | 33 | void ShareBuffer(Item **duplicate, bool *large_alloc) { | |
| 78 | 33 | *duplicate = buffer_; | |
| 79 | 33 | *large_alloc = large_alloc_; | |
| 80 | 33 | shared_buffer_ = true; | |
| 81 | 33 | } | |
| 82 | |||
| 83 | 4296 | void DoubleCapacity() { | |
| 84 | 4296 | Item *old_buffer = buffer_; | |
| 85 | 4296 | const bool old_large_alloc = large_alloc_; | |
| 86 | |||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4038 times.
|
4296 | assert(capacity_ > 0); |
| 88 | 4296 | buffer_ = Alloc(capacity_ * 2); | |
| 89 |
2/2✓ Branch 0 taken 2214740617 times.
✓ Branch 1 taken 4038 times.
|
2214788257 | for (size_t i = 0; i < size_; ++i) |
| 90 |
0/2✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2214783961 | new (buffer_ + i) Item(old_buffer[i]); |
| 91 | |||
| 92 | 4296 | FreeBuffer(old_buffer, size_, old_large_alloc); | |
| 93 | 4296 | } | |
| 94 | |||
| 95 | 43235 | void ShrinkIfOversized() { | |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43235 times.
|
43235 | assert(!shared_buffer_); |
| 97 | |||
| 98 |
2/2✓ Branch 0 taken 900 times.
✓ Branch 1 taken 42335 times.
|
43235 | if (size_ <= kNumInit) |
| 99 | 900 | return; | |
| 100 |
2/2✓ Branch 0 taken 42130 times.
✓ Branch 1 taken 205 times.
|
42335 | if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_))) |
| 101 | 42130 | return; | |
| 102 | |||
| 103 | 205 | bool const old_large_alloc = large_alloc_; | |
| 104 | 205 | Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_)); | |
| 105 |
2/2✓ Branch 0 taken 23768 times.
✓ Branch 1 taken 205 times.
|
23973 | for (size_t i = 0; i < size_; ++i) |
| 106 | 23768 | new (new_buffer + i) Item(buffer_[i]); | |
| 107 | 205 | FreeBuffer(buffer_, size_, old_large_alloc); | |
| 108 | 205 | buffer_ = new_buffer; | |
| 109 | } | ||
| 110 | |||
| 111 | // Careful! Only for externally modified buffer. | ||
| 112 | 43235 | void SetSize(const size_t new_size) { | |
| 113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43235 times.
|
43235 | assert(new_size <= capacity_); |
| 114 | 43235 | size_ = new_size; | |
| 115 | 43235 | } | |
| 116 | |||
| 117 | 1368392 | size_t size() const { return size_; } | |
| 118 | 528 | 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 | 1659064 | Item *Alloc(const size_t num_elements) { | |
| 125 | Item *result; | ||
| 126 | 1659064 | const size_t num_bytes = sizeof(Item) * num_elements; | |
| 127 |
2/2✓ Branch 0 taken 1462 times.
✓ Branch 1 taken 830531 times.
|
1659064 | if (num_bytes >= kMmapThreshold) { |
| 128 | 1505 | result = static_cast<Item *>(smmap(num_bytes)); | |
| 129 | 1505 | large_alloc_ = true; | |
| 130 | } else { | ||
| 131 | 1657559 | result = static_cast<Item *>(smalloc(num_bytes)); | |
| 132 | 1657209 | large_alloc_ = false; | |
| 133 | } | ||
| 134 | 1658714 | capacity_ = num_elements; | |
| 135 | 1658714 | return result; | |
| 136 | } | ||
| 137 | |||
| 138 | 1655474 | void Dealloc() { | |
| 139 | 1655474 | FreeBuffer(buffer_, size_, large_alloc_); | |
| 140 | 1656152 | buffer_ = NULL; | |
| 141 | 1656152 | capacity_ = 0; | |
| 142 | 1656152 | size_ = 0; | |
| 143 | 1656152 | } | |
| 144 | |||
| 145 | 1659955 | void FreeBuffer(Item *buf, const size_t size, const bool large) { | |
| 146 |
2/2✓ Branch 0 taken 3204842522 times.
✓ Branch 1 taken 832076 times.
|
3206602215 | for (size_t i = 0; i < size; ++i) |
| 147 | 3204942260 | buf[i].~Item(); | |
| 148 | |||
| 149 |
1/2✓ Branch 0 taken 832106 times.
✗ Branch 1 not taken.
|
1659955 | if (buf) { |
| 150 |
2/2✓ Branch 0 taken 1363 times.
✓ Branch 1 taken 830743 times.
|
1660015 | if (large) { |
| 151 | 1406 | smunmap(buf); | |
| 152 | } else { | ||
| 153 | 1658609 | free(buf); | |
| 154 | } | ||
| 155 | } | ||
| 156 | 1659955 | } | |
| 157 | |||
| 158 | 505078 | void CopyFrom(const BigVector<Item> &other) { | |
| 159 | 505078 | buffer_ = Alloc(other.capacity_); | |
| 160 |
2/2✓ Branch 0 taken 660000823 times.
✓ Branch 1 taken 505119 times.
|
660505942 | for (size_t i = 0; i < other.size_; ++i) { |
| 161 | 660000823 | new (buffer_ + i) Item(*other.AtPtr(i)); | |
| 162 | } | ||
| 163 | 505119 | size_ = other.size_; | |
| 164 | 505119 | shared_buffer_ = false; | |
| 165 | 505119 | } | |
| 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 |