GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-04-26 02:35:59
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 7551 BigVector() {
19 7551 buffer_ = Alloc(kNumInit);
20 7551 size_ = 0;
21 7551 shared_buffer_ = false;
22 7551 }
23
24 9853209 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9853209 times.
9853209 assert(num_items > 0);
26 9853209 buffer_ = Alloc(num_items);
27 9853209 size_ = 0;
28 9853209 shared_buffer_ = false;
29 9853209 }
30
31 19502253 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 127 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
127 if (!shared_buffer_)
38 127 Dealloc();
39 127 CopyFrom(other);
40 127 return *this;
41 }
42
43 58682104 ~BigVector() {
44
1/2
✓ Branch 0 taken 29341342 times.
✗ Branch 1 not taken.
58682104 if (!shared_buffer_)
45 58682465 Dealloc();
46 58704372 }
47
48 1470571443 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1470439406 times.
1470571443 assert(index < size_);
50 1470571443 return buffer_[index];
51 }
52
53 980067824 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 980033912 times.
980067824 assert(index < size_);
55 980067824 return &buffer_[index];
56 }
57
58 1960367485 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 8591 times.
✓ Branch 1 taken 1960314232 times.
1960367485 if (size_ == capacity_)
60 8855 DoubleCapacity();
61
1/2
✓ Branch 2 taken 448 times.
✗ Branch 3 not taken.
1960367485 new (buffer_ + size_) Item(item);
62 1960367485 size_++;
63 1960367485 }
64
65 93171 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93171 times.
93171 assert(index < size_);
67 93171 buffer_[index] = item;
68 93171 }
69
70 144 bool IsEmpty() const { return size_ == 0; }
71
72 200 void Clear() {
73 200 Dealloc();
74 200 buffer_ = Alloc(kNumInit);
75 200 }
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 8855 void DoubleCapacity() {
84 8855 Item *old_buffer = buffer_;
85 8855 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8591 times.
8855 assert(capacity_ > 0);
88 8855 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 3288647052 times.
✓ Branch 1 taken 8591 times.
3288700259 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3288691404 new (buffer_ + i) Item(old_buffer[i]);
91
92 8855 FreeBuffer(old_buffer, size_, old_large_alloc);
93 8855 }
94
95 44269 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44269 times.
44269 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 919 times.
✓ Branch 1 taken 43350 times.
44269 if (size_ <= kNumInit)
99 919 return;
100
2/2
✓ Branch 0 taken 43125 times.
✓ Branch 1 taken 225 times.
43350 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 43125 return;
102
103 225 bool old_large_alloc = large_alloc_;
104 225 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 25844 times.
✓ Branch 1 taken 225 times.
26069 for (size_t i = 0; i < size_; ++i)
106 25844 new (new_buffer + i) Item(buffer_[i]);
107 225 FreeBuffer(buffer_, size_, old_large_alloc);
108 225 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 44269 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44269 times.
44269 assert(new_size <= capacity_);
114 44269 size_ = new_size;
115 44269 }
116
117 39356500 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 58712277 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 58712277 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 2151 times.
✓ Branch 1 taken 29358459 times.
58712277 if (num_bytes >= kMmapThreshold) {
128 2195 result = static_cast<Item *>(smmap(num_bytes));
129 2195 large_alloc_ = true;
130 } else {
131 58710082 result = static_cast<Item *>(smalloc(num_bytes));
132 58709146 large_alloc_ = false;
133 }
134 58711341 capacity_ = num_elements;
135 58711341 return result;
136 }
137
138 58678580 void Dealloc() {
139 58678580 FreeBuffer(buffer_, size_, large_alloc_);
140 58706876 buffer_ = NULL;
141 58706876 capacity_ = 0;
142 58706876 size_ = 0;
143 58706876 }
144
145 58692236 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 4758934641 times.
✓ Branch 1 taken 29349535 times.
4817869173 for (size_t i = 0; i < size; ++i)
147 4759176937 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 29350162 times.
✗ Branch 1 not taken.
58692236 if (buf) {
150
2/2
✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 29348158 times.
58693490 if (large) {
151 2048 smunmap(buf);
152 } else {
153 58691442 free(buf);
154 }
155 }
156 58692236 }
157
158 19502887 void CopyFrom(const BigVector<Item> &other) {
159 19502887 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 980032097 times.
✓ Branch 1 taken 19502068 times.
999534165 for (size_t i = 0; i < other.size_; ++i) {
161 980032097 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 19502068 size_ = other.size_;
164 19502068 shared_buffer_ = false;
165 19502068 }
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