GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-04-05 02:35:23
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 6909 BigVector() {
19 6909 buffer_ = Alloc(kNumInit);
20 6909 size_ = 0;
21 6909 shared_buffer_ = false;
22 6909 }
23
24 1041388 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1041388 times.
1041388 assert(num_items > 0);
26 1041388 buffer_ = Alloc(num_items);
27 1041388 size_ = 0;
28 1041388 shared_buffer_ = false;
29 1041388 }
30
31 2001560 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 6088840 ~BigVector() {
44
2/2
✓ Branch 0 taken 3044514 times.
✓ Branch 1 taken 5 times.
6088840 if (!shared_buffer_)
45 6088879 Dealloc();
46 6089784 }
47
48 1470494763 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1470404796 times.
1470494763 assert(index < size_);
50 1470494763 return buffer_[index];
51 }
52
53 980010140 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 980005070 times.
980010140 assert(index < size_);
55 980010140 return &buffer_[index];
56 }
57
58 1960303097 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 7120 times.
✓ Branch 1 taken 1960265740 times.
1960303097 if (size_ == capacity_)
60 7300 DoubleCapacity();
61
1/2
✓ Branch 2 taken 91 times.
✗ Branch 3 not taken.
1960303097 new (buffer_ + size_) Item(item);
62 1960303097 size_++;
63 1960303097 }
64
65 79117 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79117 times.
79117 assert(index < size_);
67 79117 buffer_[index] = item;
68 79117 }
69
70 9 bool IsEmpty() const { return size_ == 0; }
71
72 158 void Clear() {
73 158 Dealloc();
74 158 buffer_ = Alloc(kNumInit);
75 158 }
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 7300 void DoubleCapacity() {
84 7300 Item *old_buffer = buffer_;
85 7300 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7120 times.
7300 assert(capacity_ > 0);
88 7300 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 3288592529 times.
✓ Branch 1 taken 7120 times.
3288630069 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3288622769 new (buffer_ + i) Item(old_buffer[i]);
91
92 7300 FreeBuffer(old_buffer, size_, old_large_alloc);
93 7300 }
94
95 30215 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30215 times.
30215 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 627 times.
✓ Branch 1 taken 29588 times.
30215 if (size_ <= kNumInit)
99 627 return;
100
2/2
✓ Branch 0 taken 29419 times.
✓ Branch 1 taken 169 times.
29588 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 29419 return;
102
103 169 bool old_large_alloc = large_alloc_;
104 169 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 19180 times.
✓ Branch 1 taken 169 times.
19349 for (size_t i = 0; i < size_; ++i)
106 19180 new (new_buffer + i) Item(buffer_[i]);
107 169 FreeBuffer(buffer_, size_, old_large_alloc);
108 169 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 30215 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30215 times.
30215 assert(new_size <= capacity_);
114 30215 size_ = new_size;
115 30215 }
116
117 4271218 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 6088693 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 6088693 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 2137 times.
✓ Branch 1 taken 3046802 times.
6088693 if (num_bytes >= kMmapThreshold) {
128 2167 result = static_cast<Item *>(smmap(num_bytes));
129 2167 large_alloc_ = true;
130 } else {
131 6086526 result = static_cast<Item *>(smalloc(num_bytes));
132 6084534 large_alloc_ = false;
133 }
134 6086701 capacity_ = num_elements;
135 6086701 return result;
136 }
137
138 6088696 void Dealloc() {
139 6088696 FreeBuffer(buffer_, size_, large_alloc_);
140 6090766 buffer_ = NULL;
141 6090766 capacity_ = 0;
142 6090766 size_ = 0;
143 6090766 }
144
145 6097354 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 4758808740 times.
✓ Branch 1 taken 3051875 times.
4765107907 for (size_t i = 0; i < size; ++i)
147 4759010553 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 3051935 times.
✗ Branch 1 not taken.
6097354 if (buf) {
150
2/2
✓ Branch 0 taken 1990 times.
✓ Branch 1 taken 3049945 times.
6097474 if (large) {
151 2020 smunmap(buf);
152 } else {
153 6095454 free(buf);
154 }
155 }
156 6097354 }
157
158 2001621 void CopyFrom(const BigVector<Item> &other) {
159 2001621 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 980003292 times.
✓ Branch 1 taken 2001697 times.
982004989 for (size_t i = 0; i < other.size_; ++i) {
161 980003292 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 2001697 size_ = other.size_;
164 2001697 shared_buffer_ = false;
165 2001697 }
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