| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/bigvector.h |
| Date: | 2026-09-13 02:40:16 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 105 | 106 | 99.1% |
| Branches: | 30 | 44 | 68.2% |
| 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 | 4098 | BigVector() { | |
| 19 | 4098 | buffer_ = Alloc(kNumInit); | |
| 20 | 4098 | size_ = 0; | |
| 21 | 4098 | shared_buffer_ = false; | |
| 22 | 4098 | } | |
| 23 | |||
| 24 | 5060965 | explicit BigVector(const size_t num_items) { | |
| 25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5060965 times.
|
5060965 | assert(num_items > 0); |
| 26 | 5060965 | buffer_ = Alloc(num_items); | |
| 27 | 5060965 | size_ = 0; | |
| 28 | 5060965 | shared_buffer_ = false; | |
| 29 | 5060965 | } | |
| 30 | |||
| 31 | 10002972 | BigVector(const BigVector<Item> &other) { CopyFrom(other); } | |
| 32 | |||
| 33 | 57 | BigVector<Item> &operator=(const BigVector<Item> &other) { | |
| 34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
|
57 | if (&other == this) |
| 35 | ✗ | return *this; | |
| 36 | |||
| 37 |
1/2✓ Branch 0 taken 57 times.
✗ Branch 1 not taken.
|
57 | if (!shared_buffer_) |
| 38 | 57 | Dealloc(); | |
| 39 | 57 | CopyFrom(other); | |
| 40 | 57 | return *this; | |
| 41 | } | ||
| 42 | |||
| 43 | 30103891 | ~BigVector() { | |
| 44 |
1/2✓ Branch 0 taken 15052226 times.
✗ Branch 1 not taken.
|
30103891 | if (!shared_buffer_) |
| 45 | 30104400 | Dealloc(); | |
| 46 | 30122163 | } | |
| 47 | |||
| 48 | 510179550 | Item At(const size_t index) const { | |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 510173549 times.
|
510179550 | assert(index < size_); |
| 50 | 510179550 | return buffer_[index]; | |
| 51 | } | ||
| 52 | |||
| 53 | 340035774 | const Item *AtPtr(const size_t index) const { | |
| 54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 340017887 times.
|
340035774 | assert(index < size_); |
| 55 | 340035774 | return &buffer_[index]; | |
| 56 | } | ||
| 57 | |||
| 58 | 680126122 | void PushBack(const Item &item) { | |
| 59 |
2/2✓ Branch 0 taken 4051 times.
✓ Branch 1 taken 680120028 times.
|
680126122 | if (size_ == capacity_) |
| 60 | 4063 | DoubleCapacity(); | |
| 61 |
1/2✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
|
680126122 | new (buffer_ + size_) Item(item); |
| 62 | 680126122 | size_++; | |
| 63 | 680126122 | } | |
| 64 | |||
| 65 | 19009 | void Replace(size_t index, const Item &item) { | |
| 66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 19009 times.
|
19009 | assert(index < size_); |
| 67 | 19009 | buffer_[index] = item; | |
| 68 | 19009 | } | |
| 69 | |||
| 70 | 167 | bool IsEmpty() const { return size_ == 0; } | |
| 71 | |||
| 72 | 55 | void Clear() { | |
| 73 | 55 | Dealloc(); | |
| 74 | 55 | buffer_ = Alloc(kNumInit); | |
| 75 | 55 | } | |
| 76 | |||
| 77 | 17 | void ShareBuffer(Item **duplicate, bool *large_alloc) { | |
| 78 | 17 | *duplicate = buffer_; | |
| 79 | 17 | *large_alloc = large_alloc_; | |
| 80 | 17 | shared_buffer_ = true; | |
| 81 | 17 | } | |
| 82 | |||
| 83 | 4063 | void DoubleCapacity() { | |
| 84 | 4063 | Item *old_buffer = buffer_; | |
| 85 | 4063 | const bool old_large_alloc = large_alloc_; | |
| 86 | |||
| 87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4051 times.
|
4063 | assert(capacity_ > 0); |
| 88 | 4063 | buffer_ = Alloc(capacity_ * 2); | |
| 89 |
2/2✓ Branch 0 taken 1140966948 times.
✓ Branch 1 taken 4051 times.
|
1140973027 | for (size_t i = 0; i < size_; ++i) |
| 90 |
0/2✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1140968964 | new (buffer_ + i) Item(old_buffer[i]); |
| 91 | |||
| 92 | 4063 | FreeBuffer(old_buffer, size_, old_large_alloc); | |
| 93 | 4063 | } | |
| 94 | |||
| 95 | 2043 | void ShrinkIfOversized() { | |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2043 times.
|
2043 | assert(!shared_buffer_); |
| 97 | |||
| 98 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 2000 times.
|
2043 | if (size_ <= kNumInit) |
| 99 | 43 | return; | |
| 100 |
2/2✓ Branch 0 taken 1975 times.
✓ Branch 1 taken 25 times.
|
2000 | if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_))) |
| 101 | 1975 | return; | |
| 102 | |||
| 103 | 25 | bool const old_large_alloc = large_alloc_; | |
| 104 | 25 | Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_)); | |
| 105 |
2/2✓ Branch 0 taken 2652 times.
✓ Branch 1 taken 25 times.
|
2677 | for (size_t i = 0; i < size_; ++i) |
| 106 | 2652 | new (new_buffer + i) Item(buffer_[i]); | |
| 107 | 25 | FreeBuffer(buffer_, size_, old_large_alloc); | |
| 108 | 25 | buffer_ = new_buffer; | |
| 109 | } | ||
| 110 | |||
| 111 | // Careful! Only for externally modified buffer. | ||
| 112 | 2043 | void SetSize(const size_t new_size) { | |
| 113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2043 times.
|
2043 | assert(new_size <= capacity_); |
| 114 | 2043 | size_ = new_size; | |
| 115 | 2043 | } | |
| 116 | |||
| 117 | 20028909 | size_t size() const { return size_; } | |
| 118 | 272 | 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 | 15069211 | Item *Alloc(const size_t num_elements) { | |
| 125 | Item *result; | ||
| 126 | 15069211 | const size_t num_bytes = sizeof(Item) * num_elements; | |
| 127 |
2/2✓ Branch 0 taken 733 times.
✓ Branch 1 taken 15067991 times.
|
15069211 | if (num_bytes >= kMmapThreshold) { |
| 128 | 735 | result = static_cast<Item *>(smmap(num_bytes)); | |
| 129 | 735 | large_alloc_ = true; | |
| 130 | } else { | ||
| 131 | 15068476 | result = static_cast<Item *>(smalloc(num_bytes)); | |
| 132 | 15068816 | large_alloc_ = false; | |
| 133 | } | ||
| 134 | 15069551 | capacity_ = num_elements; | |
| 135 | 15069551 | return result; | |
| 136 | } | ||
| 137 | |||
| 138 | 30103302 | void Dealloc() { | |
| 139 | 30103302 | FreeBuffer(buffer_, size_, large_alloc_); | |
| 140 | 30124112 | buffer_ = NULL; | |
| 141 | 30124112 | capacity_ = 0; | |
| 142 | 30124112 | size_ = 0; | |
| 143 | 30124112 | } | |
| 144 | |||
| 145 | 15057881 | void FreeBuffer(Item *buf, const size_t size, const bool large) { | |
| 146 |
2/2✓ Branch 0 taken 1651091046 times.
✓ Branch 1 taken 15055624 times.
|
1666258248 | for (size_t i = 0; i < size; ++i) |
| 147 | 1651200367 | buf[i].~Item(); | |
| 148 | |||
| 149 |
1/2✓ Branch 0 taken 15056061 times.
✗ Branch 1 not taken.
|
15057881 | if (buf) { |
| 150 |
2/2✓ Branch 0 taken 682 times.
✓ Branch 1 taken 15055379 times.
|
15058318 | if (large) { |
| 151 | 684 | smunmap(buf); | |
| 152 | } else { | ||
| 153 | 15057634 | free(buf); | |
| 154 | } | ||
| 155 | } | ||
| 156 | 15057881 | } | |
| 157 | |||
| 158 | 10003149 | void CopyFrom(const BigVector<Item> &other) { | |
| 159 | 10003149 | buffer_ = Alloc(other.capacity_); | |
| 160 |
2/2✓ Branch 0 taken 340016460 times.
✓ Branch 1 taken 10003089 times.
|
350019549 | for (size_t i = 0; i < other.size_; ++i) { |
| 161 | 340016460 | new (buffer_ + i) Item(*other.AtPtr(i)); | |
| 162 | } | ||
| 163 | 10003089 | size_ = other.size_; | |
| 164 | 10003089 | shared_buffer_ = false; | |
| 165 | 10003089 | } | |
| 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 |