Directory: | cvmfs/ |
---|---|
File: | cvmfs/bigvector.h |
Date: | 2025-07-21 10:50:29 |
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 | 5118 | BigVector() { | |
19 | 5118 | buffer_ = Alloc(kNumInit); | |
20 | 5118 | size_ = 0; | |
21 | 5118 | shared_buffer_ = false; | |
22 | 5118 | } | |
23 | |||
24 | 5063693 | explicit BigVector(const size_t num_items) { | |
25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5063693 times.
|
5063693 | assert(num_items > 0); |
26 | 5063693 | buffer_ = Alloc(num_items); | |
27 | 5063693 | size_ = 0; | |
28 | 5063693 | shared_buffer_ = false; | |
29 | 5063693 | } | |
30 | |||
31 | 10003285 | BigVector(const BigVector<Item> &other) { CopyFrom(other); } | |
32 | |||
33 | 89 | BigVector<Item> &operator=(const BigVector<Item> &other) { | |
34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 89 times.
|
89 | if (&other == this) |
35 | ✗ | return *this; | |
36 | |||
37 |
1/2✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
|
89 | if (!shared_buffer_) |
38 | 89 | Dealloc(); | |
39 | 89 | CopyFrom(other); | |
40 | 89 | return *this; | |
41 | } | ||
42 | |||
43 | 30125076 | ~BigVector() { | |
44 |
1/2✓ Branch 0 taken 15062739 times.
✗ Branch 1 not taken.
|
30125076 | if (!shared_buffer_) |
45 | 30125279 | Dealloc(); | |
46 | 30129090 | } | |
47 | |||
48 | 1470414826 | Item At(const size_t index) const { | |
49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1470330973 times.
|
1470414826 | assert(index < size_); |
50 | 1470414826 | return buffer_[index]; | |
51 | } | ||
52 | |||
53 | 980036280 | const Item *AtPtr(const size_t index) const { | |
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 980018140 times.
|
980036280 | assert(index < size_); |
55 | 980036280 | return &buffer_[index]; | |
56 | } | ||
57 | |||
58 | 1960289922 | void PushBack(const Item &item) { | |
59 |
2/2✓ Branch 0 taken 7036 times.
✓ Branch 1 taken 1960254723 times.
|
1960289922 | if (size_ == capacity_) |
60 | 7204 | DoubleCapacity(); | |
61 |
1/2✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
1960289922 | new (buffer_ + size_) Item(item); |
62 | 1960289922 | size_++; | |
63 | 1960289922 | } | |
64 | |||
65 | 77107 | void Replace(size_t index, const Item &item) { | |
66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 77107 times.
|
77107 | assert(index < size_); |
67 | 77107 | buffer_[index] = item; | |
68 | 77107 | } | |
69 | |||
70 | 3 | bool IsEmpty() const { return size_ == 0; } | |
71 | |||
72 | 188 | void Clear() { | |
73 | 188 | Dealloc(); | |
74 | 188 | buffer_ = Alloc(kNumInit); | |
75 | 188 | } | |
76 | |||
77 | 49 | void ShareBuffer(Item **duplicate, bool *large_alloc) { | |
78 | 49 | *duplicate = buffer_; | |
79 | 49 | *large_alloc = large_alloc_; | |
80 | 49 | shared_buffer_ = true; | |
81 | 49 | } | |
82 | |||
83 | 7204 | void DoubleCapacity() { | |
84 | 7204 | Item *old_buffer = buffer_; | |
85 | 7204 | const bool old_large_alloc = large_alloc_; | |
86 | |||
87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7036 times.
|
7204 | assert(capacity_ > 0); |
88 | 7204 | buffer_ = Alloc(capacity_ * 2); | |
89 |
2/2✓ Branch 0 taken 3288588448 times.
✓ Branch 1 taken 7036 times.
|
3288623876 | for (size_t i = 0; i < size_; ++i) |
90 |
0/2✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3288616672 | new (buffer_ + i) Item(old_buffer[i]); |
91 | |||
92 | 7204 | FreeBuffer(old_buffer, size_, old_large_alloc); | |
93 | 7204 | } | |
94 | |||
95 | 28205 | void ShrinkIfOversized() { | |
96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28205 times.
|
28205 | assert(!shared_buffer_); |
97 | |||
98 |
2/2✓ Branch 0 taken 583 times.
✓ Branch 1 taken 27622 times.
|
28205 | if (size_ <= kNumInit) |
99 | 583 | return; | |
100 |
2/2✓ Branch 0 taken 27461 times.
✓ Branch 1 taken 161 times.
|
27622 | if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_))) |
101 | 27461 | return; | |
102 | |||
103 | 161 | bool old_large_alloc = large_alloc_; | |
104 | 161 | Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_)); | |
105 |
2/2✓ Branch 0 taken 18228 times.
✓ Branch 1 taken 161 times.
|
18389 | for (size_t i = 0; i < size_; ++i) |
106 | 18228 | new (new_buffer + i) Item(buffer_[i]); | |
107 | 161 | FreeBuffer(buffer_, size_, old_large_alloc); | |
108 | 161 | buffer_ = new_buffer; | |
109 | } | ||
110 | |||
111 | // Careful! Only for externally modified buffer. | ||
112 | 28205 | void SetSize(const size_t new_size) { | |
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 28205 times.
|
28205 | assert(new_size <= capacity_); |
114 | 28205 | size_ = new_size; | |
115 | 28205 | } | |
116 | |||
117 | 20268696 | size_t size() const { return size_; } | |
118 | 784 | 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 | 30108702 | Item *Alloc(const size_t num_elements) { | |
125 | Item *result; | ||
126 | 30108702 | const size_t num_bytes = sizeof(Item) * num_elements; | |
127 |
2/2✓ Branch 0 taken 2135 times.
✓ Branch 1 taken 15056028 times.
|
30108702 | if (num_bytes >= kMmapThreshold) { |
128 | 2163 | result = static_cast<Item *>(smmap(num_bytes)); | |
129 | 2163 | large_alloc_ = true; | |
130 | } else { | ||
131 | 30106539 | result = static_cast<Item *>(smalloc(num_bytes)); | |
132 | 30101219 | large_alloc_ = false; | |
133 | } | ||
134 | 30103382 | capacity_ = num_elements; | |
135 | 30103382 | return result; | |
136 | } | ||
137 | |||
138 | 30124704 | void Dealloc() { | |
139 | 30124704 | FreeBuffer(buffer_, size_, large_alloc_); | |
140 | 30131826 | buffer_ = NULL; | |
141 | 30131826 | capacity_ = 0; | |
142 | 30131826 | size_ = 0; | |
143 | 30131826 | } | |
144 | |||
145 | 30133267 | void FreeBuffer(Item *buf, const size_t size, const bool large) { | |
146 |
2/2✓ Branch 0 taken 4758807709 times.
✓ Branch 1 taken 15069777 times.
|
4789081167 | for (size_t i = 0; i < size; ++i) |
147 | 4758947900 | buf[i].~Item(); | |
148 | |||
149 |
1/2✓ Branch 0 taken 15069859 times.
✗ Branch 1 not taken.
|
30133267 | if (buf) { |
150 |
2/2✓ Branch 0 taken 1988 times.
✓ Branch 1 taken 15067871 times.
|
30133431 | if (large) { |
151 | 2016 | smunmap(buf); | |
152 | } else { | ||
153 | 30131415 | free(buf); | |
154 | } | ||
155 | } | ||
156 | 30133267 | } | |
157 | |||
158 | 10003294 | void CopyFrom(const BigVector<Item> &other) { | |
159 | 10003294 | buffer_ = Alloc(other.capacity_); | |
160 |
2/2✓ Branch 0 taken 980016460 times.
✓ Branch 1 taken 10003334 times.
|
990019794 | for (size_t i = 0; i < other.size_; ++i) { |
161 | 980016460 | new (buffer_ + i) Item(*other.AtPtr(i)); | |
162 | } | ||
163 | 10003334 | size_ = other.size_; | |
164 | 10003334 | shared_buffer_ = false; | |
165 | 10003334 | } | |
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 |