Directory: | cvmfs/ |
---|---|
File: | cvmfs/bigvector.h |
Date: | 2025-06-01 02:36:00 |
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 | 227 | BigVector() { | |
19 | 227 | buffer_ = Alloc(kNumInit); | |
20 | 227 | size_ = 0; | |
21 | 227 | shared_buffer_ = false; | |
22 | 227 | } | |
23 | |||
24 | 253411 | explicit BigVector(const size_t num_items) { | |
25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 253411 times.
|
253411 | assert(num_items > 0); |
26 | 253411 | buffer_ = Alloc(num_items); | |
27 | 253411 | size_ = 0; | |
28 | 253411 | shared_buffer_ = false; | |
29 | 253411 | } | |
30 | |||
31 | 500169 | BigVector(const BigVector<Item> &other) { CopyFrom(other); } | |
32 | |||
33 | 3 | BigVector<Item> &operator=(const BigVector<Item> &other) { | |
34 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (&other == this) |
35 | ✗ | return *this; | |
36 | |||
37 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (!shared_buffer_) |
38 | 3 | Dealloc(); | |
39 | 3 | CopyFrom(other); | |
40 | 3 | return *this; | |
41 | } | ||
42 | |||
43 | 1506803 | ~BigVector() { | |
44 |
1/2✓ Branch 0 taken 753415 times.
✗ Branch 1 not taken.
|
1506803 | if (!shared_buffer_) |
45 | 1506824 | Dealloc(); | |
46 | 1506993 | } | |
47 | |||
48 | 30014792 | Item At(const size_t index) const { | |
49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30011794 times.
|
30014792 | assert(index < size_); |
50 | 30014792 | return buffer_[index]; | |
51 | } | ||
52 | |||
53 | 20001744 | const Item *AtPtr(const size_t index) const { | |
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20000872 times.
|
20001744 | assert(index < size_); |
55 | 20001744 | return &buffer_[index]; | |
56 | } | ||
57 | |||
58 | 40009641 | void PushBack(const Item &item) { | |
59 |
2/2✓ Branch 0 taken 216 times.
✓ Branch 1 taken 40008409 times.
|
40009641 | if (size_ == capacity_) |
60 | 222 | DoubleCapacity(); | |
61 |
1/2✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
40009641 | new (buffer_ + size_) Item(item); |
62 | 40009641 | size_++; | |
63 | 40009641 | } | |
64 | |||
65 | 2004 | void Replace(size_t index, const Item &item) { | |
66 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2004 times.
|
2004 | assert(index < size_); |
67 | 2004 | buffer_[index] = item; | |
68 | 2004 | } | |
69 | |||
70 | 3 | bool IsEmpty() const { return size_ == 0; } | |
71 | |||
72 | 5 | void Clear() { | |
73 | 5 | Dealloc(); | |
74 | 5 | buffer_ = Alloc(kNumInit); | |
75 | 5 | } | |
76 | |||
77 | 1 | void ShareBuffer(Item **duplicate, bool *large_alloc) { | |
78 | 1 | *duplicate = buffer_; | |
79 | 1 | *large_alloc = large_alloc_; | |
80 | 1 | shared_buffer_ = true; | |
81 | 1 | } | |
82 | |||
83 | 222 | void DoubleCapacity() { | |
84 | 222 | Item *old_buffer = buffer_; | |
85 | 222 | bool old_large_alloc = large_alloc_; | |
86 | |||
87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 216 times.
|
222 | assert(capacity_ > 0); |
88 | 222 | buffer_ = Alloc(capacity_ * 2); | |
89 |
2/2✓ Branch 0 taken 67117160 times.
✓ Branch 1 taken 216 times.
|
67118390 | for (size_t i = 0; i < size_; ++i) |
90 |
0/2✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
67118168 | new (buffer_ + i) Item(old_buffer[i]); |
91 | |||
92 | 222 | FreeBuffer(old_buffer, size_, old_large_alloc); | |
93 | 222 | } | |
94 | |||
95 | 1006 | void ShrinkIfOversized() { | |
96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1006 times.
|
1006 | assert(!shared_buffer_); |
97 | |||
98 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 985 times.
|
1006 | if (size_ <= kNumInit) |
99 | 21 | return; | |
100 |
2/2✓ Branch 0 taken 980 times.
✓ Branch 1 taken 5 times.
|
985 | if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_))) |
101 | 980 | return; | |
102 | |||
103 | 5 | bool old_large_alloc = large_alloc_; | |
104 | 5 | Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_)); | |
105 |
2/2✓ Branch 0 taken 576 times.
✓ Branch 1 taken 5 times.
|
581 | for (size_t i = 0; i < size_; ++i) |
106 | 576 | new (new_buffer + i) Item(buffer_[i]); | |
107 | 5 | FreeBuffer(buffer_, size_, old_large_alloc); | |
108 | 5 | buffer_ = new_buffer; | |
109 | } | ||
110 | |||
111 | // Careful! Only for externally modified buffer. | ||
112 | 1006 | void SetSize(const size_t new_size) { | |
113 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1006 times.
|
1006 | assert(new_size <= capacity_); |
114 | 1006 | size_ = new_size; | |
115 | 1006 | } | |
116 | |||
117 | 1009541 | size_t size() const { return size_; } | |
118 | 16 | 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 | 1505396 | Item *Alloc(const size_t num_elements) { | |
125 | Item *result; | ||
126 | 1505396 | size_t num_bytes = sizeof(Item) * num_elements; | |
127 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 752768 times.
|
1505396 | if (num_bytes >= kMmapThreshold) { |
128 | 45 | result = static_cast<Item *>(smmap(num_bytes)); | |
129 | 45 | large_alloc_ = true; | |
130 | } else { | ||
131 | 1505351 | result = static_cast<Item *>(smalloc(num_bytes)); | |
132 | 1505995 | large_alloc_ = false; | |
133 | } | ||
134 | 1506040 | capacity_ = num_elements; | |
135 | 1506040 | return result; | |
136 | } | ||
137 | |||
138 | 1506762 | void Dealloc() { | |
139 | 1506762 | FreeBuffer(buffer_, size_, large_alloc_); | |
140 | 1507066 | buffer_ = NULL; | |
141 | 1507066 | capacity_ = 0; | |
142 | 1507066 | size_ = 0; | |
143 | 1507066 | } | |
144 | |||
145 | 1507051 | void FreeBuffer(Item *buf, const size_t size, const bool large) { | |
146 |
2/2✓ Branch 0 taken 97125204 times.
✓ Branch 1 taken 753604 times.
|
98639413 | for (size_t i = 0; i < size; ++i) |
147 | 97132362 | buf[i].~Item(); | |
148 | |||
149 |
1/2✓ Branch 0 taken 753616 times.
✗ Branch 1 not taken.
|
1507051 | if (buf) { |
150 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 753575 times.
|
1507075 | if (large) { |
151 | 42 | smunmap(buf); | |
152 | } else { | ||
153 | 1507033 | free(buf); | |
154 | } | ||
155 | } | ||
156 | 1507051 | } | |
157 | |||
158 | 500174 | void CopyFrom(const BigVector<Item> &other) { | |
159 | 500174 | buffer_ = Alloc(other.capacity_); | |
160 |
2/2✓ Branch 0 taken 20000823 times.
✓ Branch 1 taken 500173 times.
|
20500996 | for (size_t i = 0; i < other.size_; ++i) { |
161 | 20000823 | new (buffer_ + i) Item(*other.AtPtr(i)); | |
162 | } | ||
163 | 500173 | size_ = other.size_; | |
164 | 500173 | shared_buffer_ = false; | |
165 | 500173 | } | |
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 |