GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-08-30 02:40:36
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 10227 BigVector() {
19 10227 buffer_ = Alloc(kNumInit);
20 10227 size_ = 0;
21 10227 shared_buffer_ = false;
22 10227 }
23
24 11147801 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11147801 times.
11147801 assert(num_items > 0);
26 11147801 buffer_ = Alloc(num_items);
27 11147801 size_ = 0;
28 11147801 shared_buffer_ = false;
29 11147801 }
30
31 22006998 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 132 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 132 times.
132 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 132 times.
✗ Branch 1 not taken.
132 if (!shared_buffer_)
38 132 Dealloc();
39 132 CopyFrom(other);
40 132 return *this;
41 }
42
43 66257176 ~BigVector() {
44
1/2
✓ Branch 0 taken 33129603 times.
✗ Branch 1 not taken.
66257176 if (!shared_buffer_)
45 66258942 Dealloc();
46 66294860 }
47
48 1320631243 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1320505376 times.
1320631243 assert(index < size_);
50 1320631243 return buffer_[index];
51 }
52
53 880077722 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 880038861 times.
880077722 assert(index < size_);
55 880077722 return &buffer_[index];
56 }
57
58 1760415348 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 9215 times.
✓ Branch 1 taken 1760363504 times.
1760415348 if (size_ == capacity_)
60 9467 DoubleCapacity();
61
1/2
✓ Branch 2 taken 425 times.
✗ Branch 3 not taken.
1760415348 new (buffer_ + size_) Item(item);
62 1760415348 size_++;
63 1760415348 }
64
65 86163 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86163 times.
86163 assert(index < size_);
67 86163 buffer_[index] = item;
68 86163 }
69
70 286 bool IsEmpty() const { return size_ == 0; }
71
72 220 void Clear() {
73 220 Dealloc();
74 220 buffer_ = Alloc(kNumInit);
75 220 }
76
77 44 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 44 *duplicate = buffer_;
79 44 *large_alloc = large_alloc_;
80 44 shared_buffer_ = true;
81 44 }
82
83 9467 void DoubleCapacity() {
84 9467 Item *old_buffer = buffer_;
85 9467 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9215 times.
9467 assert(capacity_ > 0);
88 9467 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 2953148598 times.
✓ Branch 1 taken 9215 times.
2953200401 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2953190934 new (buffer_ + i) Item(old_buffer[i]);
91
92 9467 FreeBuffer(old_buffer, size_, old_large_alloc);
93 9467 }
94
95 42251 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42251 times.
42251 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 877 times.
✓ Branch 1 taken 41374 times.
42251 if (size_ <= kNumInit)
99 877 return;
100
2/2
✓ Branch 0 taken 41162 times.
✓ Branch 1 taken 212 times.
41374 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 41162 return;
102
103 212 bool const old_large_alloc = large_alloc_;
104 212 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 24392 times.
✓ Branch 1 taken 212 times.
24604 for (size_t i = 0; i < size_; ++i)
106 24392 new (new_buffer + i) Item(buffer_[i]);
107 212 FreeBuffer(buffer_, size_, old_large_alloc);
108 212 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 42251 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42251 times.
42251 assert(new_size <= capacity_);
114 42251 size_ = new_size;
115 42251 }
116
117 44396324 size_t size() const { return size_; }
118 704 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 33168080 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 33168080 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 1934 times.
✓ Branch 1 taken 33164729 times.
33168080 if (num_bytes >= kMmapThreshold) {
128 1976 result = static_cast<Item *>(smmap(num_bytes));
129 1976 large_alloc_ = true;
130 } else {
131 33166104 result = static_cast<Item *>(smalloc(num_bytes));
132 33165796 large_alloc_ = false;
133 }
134 33167772 capacity_ = num_elements;
135 33167772 return result;
136 }
137
138 66253662 void Dealloc() {
139 66253662 FreeBuffer(buffer_, size_, large_alloc_);
140 66298858 buffer_ = NULL;
141 66298858 capacity_ = 0;
142 66298858 size_ = 0;
143 66298858 }
144
145 33140025 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 4273495754 times.
✓ Branch 1 taken 33135910 times.
4306860572 for (size_t i = 0; i < size; ++i)
147 4273720547 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 33137601 times.
✗ Branch 1 not taken.
33140025 if (buf) {
150
2/2
✓ Branch 0 taken 1802 times.
✓ Branch 1 taken 33135799 times.
33141716 if (large) {
151 1844 smunmap(buf);
152 } else {
153 33139872 free(buf);
154 }
155 }
156 33140025 }
157
158 22007350 void CopyFrom(const BigVector<Item> &other) {
159 22007350 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 880036212 times.
✓ Branch 1 taken 22006382 times.
902042594 for (size_t i = 0; i < other.size_; ++i) {
161 880036212 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 22006382 size_ = other.size_;
164 22006382 shared_buffer_ = false;
165 22006382 }
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