GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2025-11-09 02:35:23
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 4313 BigVector() {
19 4313 buffer_ = Alloc(kNumInit);
20 4313 size_ = 0;
21 4313 shared_buffer_ = false;
22 4313 }
23
24 7341859 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7341859 times.
7341859 assert(num_items > 0);
26 7341859 buffer_ = Alloc(num_items);
27 7341859 size_ = 0;
28 7341859 shared_buffer_ = false;
29 7341859 }
30
31 14503818 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 75 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75 times.
75 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 75 times.
✗ Branch 1 not taken.
75 if (!shared_buffer_)
38 75 Dealloc();
39 75 CopyFrom(other);
40 75 return *this;
41 }
42
43 43668789 ~BigVector() {
44
1/2
✓ Branch 0 taken 21834620 times.
✗ Branch 1 not taken.
43668789 if (!shared_buffer_)
45 43669178 Dealloc();
46 43674969 }
47
48 510228090 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 510183164 times.
510228090 assert(index < size_);
50 510228090 return buffer_[index];
51 }
52
53 340049080 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 340024540 times.
340049080 assert(index < size_);
55 340049080 return &buffer_[index];
56 }
57
58 680144769 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 4203 times.
✓ Branch 1 taken 680125336 times.
680144769 if (size_ == capacity_)
60 4293 DoubleCapacity();
61
1/2
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
680144769 new (buffer_ + size_) Item(item);
62 680144769 size_++;
63 680144769 }
64
65 32061 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32061 times.
32061 assert(index < size_);
67 32061 buffer_[index] = item;
68 32061 }
69
70 27 bool IsEmpty() const { return size_ == 0; }
71
72 61 void Clear() {
73 61 Dealloc();
74 61 buffer_ = Alloc(kNumInit);
75 61 }
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 4293 void DoubleCapacity() {
84 4293 Item *old_buffer = buffer_;
85 4293 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4203 times.
4293 assert(capacity_ > 0);
88 4293 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 1140975505 times.
✓ Branch 1 taken 4203 times.
1140994918 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1140990625 new (buffer_ + i) Item(old_buffer[i]);
91
92 4293 FreeBuffer(old_buffer, size_, old_large_alloc);
93 4293 }
94
95 15095 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15095 times.
15095 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 316 times.
✓ Branch 1 taken 14779 times.
15095 if (size_ <= kNumInit)
99 316 return;
100
2/2
✓ Branch 0 taken 14702 times.
✓ Branch 1 taken 77 times.
14779 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 14702 return;
102
103 77 bool old_large_alloc = large_alloc_;
104 77 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 8840 times.
✓ Branch 1 taken 77 times.
8917 for (size_t i = 0; i < size_; ++i)
106 8840 new (new_buffer + i) Item(buffer_[i]);
107 77 FreeBuffer(buffer_, size_, old_large_alloc);
108 77 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 15095 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15095 times.
15095 assert(new_size <= capacity_);
114 15095 size_ = new_size;
115 15095 }
116
117 29141065 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 43623914 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 43623914 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 746 times.
✓ Branch 1 taken 21813103 times.
43623914 if (num_bytes >= kMmapThreshold) {
128 761 result = static_cast<Item *>(smmap(num_bytes));
129 761 large_alloc_ = true;
130 } else {
131 43623153 result = static_cast<Item *>(smalloc(num_bytes));
132 43619151 large_alloc_ = false;
133 }
134 43619912 capacity_ = num_elements;
135 43619912 return result;
136 }
137
138 43666224 void Dealloc() {
139 43666224 FreeBuffer(buffer_, size_, large_alloc_);
140 43678532 buffer_ = NULL;
141 43678532 capacity_ = 0;
142 43678532 size_ = 0;
143 43678532 }
144
145 43671355 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 1651105571 times.
✓ Branch 1 taken 21836901 times.
1694924497 for (size_t i = 0; i < size; ++i)
147 1651253142 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 21836930 times.
✗ Branch 1 not taken.
43671355 if (buf) {
150
2/2
✓ Branch 0 taken 695 times.
✓ Branch 1 taken 21836235 times.
43671413 if (large) {
151 710 smunmap(buf);
152 } else {
153 43670703 free(buf);
154 }
155 }
156 43671355 }
157
158 14503893 void CopyFrom(const BigVector<Item> &other) {
159 14503893 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 340023867 times.
✓ Branch 1 taken 14504444 times.
354528311 for (size_t i = 0; i < other.size_; ++i) {
161 340023867 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 14504444 size_ = other.size_;
164 14504444 shared_buffer_ = false;
165 14504444 }
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