GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-05-24 02:35:55
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 5182 BigVector() {
19 5182 buffer_ = Alloc(kNumInit);
20 5182 size_ = 0;
21 5182 shared_buffer_ = false;
22 5182 }
23
24 2569642 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2569642 times.
2569642 assert(num_items > 0);
26 2569642 buffer_ = Alloc(num_items);
27 2569642 size_ = 0;
28 2569642 shared_buffer_ = false;
29 2569642 }
30
31 5003203 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 68 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 if (!shared_buffer_)
38 68 Dealloc();
39 68 CopyFrom(other);
40 68 return *this;
41 }
42
43 15131461 ~BigVector() {
44
1/2
✓ Branch 0 taken 7565883 times.
✗ Branch 1 not taken.
15131461 if (!shared_buffer_)
45 15131653 Dealloc();
46 15137315 }
47
48 1440347623 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1440296698 times.
1440347623 assert(index < size_);
50 1440347623 return buffer_[index];
51 }
52
53 960017516 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 960008758 times.
960017516 assert(index < size_);
55 960017516 return &buffer_[index];
56 }
57
58 1920231405 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 6586 times.
✓ Branch 1 taken 1920207449 times.
1920231405 if (size_ == capacity_)
60 6688 DoubleCapacity();
61
1/2
✓ Branch 2 taken 291 times.
✗ Branch 3 not taken.
1920231405 new (buffer_ + size_) Item(item);
62 1920231405 size_++;
63 1920231405 }
64
65 65063 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 65063 times.
65063 assert(index < size_);
67 65063 buffer_[index] = item;
68 65063 }
69
70 81 bool IsEmpty() const { return size_ == 0; }
71
72 135 void Clear() {
73 135 Dealloc();
74 135 buffer_ = Alloc(kNumInit);
75 135 }
76
77 48 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 48 *duplicate = buffer_;
79 48 *large_alloc = large_alloc_;
80 48 shared_buffer_ = true;
81 48 }
82
83 6688 void DoubleCapacity() {
84 6688 Item *old_buffer = buffer_;
85 6688 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6586 times.
6688 assert(capacity_ > 0);
88 6688 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 3221429405 times.
✓ Branch 1 taken 6586 times.
3221453229 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3221446541 new (buffer_ + i) Item(old_buffer[i]);
91
92 6688 FreeBuffer(old_buffer, size_, old_large_alloc);
93 6688 }
94
95 17159 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17159 times.
17159 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 16807 times.
17159 if (size_ <= kNumInit)
99 352 return;
100
2/2
✓ Branch 0 taken 16691 times.
✓ Branch 1 taken 116 times.
16807 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 16691 return;
102
103 116 bool old_large_alloc = large_alloc_;
104 116 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 12892 times.
✓ Branch 1 taken 116 times.
13008 for (size_t i = 0; i < size_; ++i)
106 12892 new (new_buffer + i) Item(buffer_[i]);
107 116 FreeBuffer(buffer_, size_, old_large_alloc);
108 116 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 17159 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17159 times.
17159 assert(new_size <= capacity_);
114 17159 size_ = new_size;
115 17159 }
116
117 10160023 size_t size() const { return size_; }
118 768 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 15124188 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 15124188 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 2081 times.
✓ Branch 1 taken 7563883 times.
15124188 if (num_bytes >= kMmapThreshold) {
128 2098 result = static_cast<Item *>(smmap(num_bytes));
129 2098 large_alloc_ = true;
130 } else {
131 15122090 result = static_cast<Item *>(smalloc(num_bytes));
132 15121470 large_alloc_ = false;
133 }
134 15123568 capacity_ = num_elements;
135 15123568 return result;
136 }
137
138 15131052 void Dealloc() {
139 15131052 FreeBuffer(buffer_, size_, large_alloc_);
140 15141340 buffer_ = NULL;
141 15141340 capacity_ = 0;
142 15141340 size_ = 0;
143 15141340 }
144
145 15139191 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 4661599450 times.
✓ Branch 1 taken 7572454 times.
4676886152 for (size_t i = 0; i < size; ++i)
147 4661746961 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 7572855 times.
✗ Branch 1 not taken.
15139191 if (buf) {
150
2/2
✓ Branch 0 taken 1937 times.
✓ Branch 1 taken 7570918 times.
15139993 if (large) {
151 1954 smunmap(buf);
152 } else {
153 15138039 free(buf);
154 }
155 }
156 15139191 }
157
158 5003341 void CopyFrom(const BigVector<Item> &other) {
159 5003341 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 960008230 times.
✓ Branch 1 taken 5003401 times.
965011631 for (size_t i = 0; i < other.size_; ++i) {
161 960008230 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 5003401 size_ = other.size_;
164 5003401 shared_buffer_ = false;
165 5003401 }
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