GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-02-01 02:35:56
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 7611 BigVector() {
19 7611 buffer_ = Alloc(kNumInit);
20 7611 size_ = 0;
21 7611 shared_buffer_ = false;
22 7611 }
23
24 9858315 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9858315 times.
9858315 assert(num_items > 0);
26 9858315 buffer_ = Alloc(num_items);
27 9858315 size_ = 0;
28 9858315 shared_buffer_ = false;
29 9858315 }
30
31 19504365 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 58702360 ~BigVector() {
44
1/2
✓ Branch 0 taken 29351780 times.
✗ Branch 1 not taken.
58702360 if (!shared_buffer_)
45 58703361 Dealloc();
46 58706240 }
47
48 1470569167 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1470437258 times.
1470569167 assert(index < size_);
50 1470569167 return buffer_[index];
51 }
52
53 980067836 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 980033918 times.
980067836 assert(index < size_);
55 980067836 return &buffer_[index];
56 }
57
58 1960360463 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 8845 times.
✓ Branch 1 taken 1960307186 times.
1960360463 if (size_ == capacity_)
60 9109 DoubleCapacity();
61
1/2
✓ Branch 2 taken 216 times.
✗ Branch 3 not taken.
1960360463 new (buffer_ + size_) Item(item);
62 1960360463 size_++;
63 1960360463 }
64
65 93173 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 93173 times.
93173 assert(index < size_);
67 93173 buffer_[index] = item;
68 93173 }
69
70 126 bool IsEmpty() const { return size_ == 0; }
71
72 188 void Clear() {
73 188 Dealloc();
74 188 buffer_ = Alloc(kNumInit);
75 188 }
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 9109 void DoubleCapacity() {
84 9109 Item *old_buffer = buffer_;
85 9109 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8845 times.
9109 assert(capacity_ > 0);
88 9109 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 3288639966 times.
✓ Branch 1 taken 8845 times.
3288693427 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3288684318 new (buffer_ + i) Item(old_buffer[i]);
91
92 9109 FreeBuffer(old_buffer, size_, old_large_alloc);
93 9109 }
94
95 44271 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44271 times.
44271 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 921 times.
✓ Branch 1 taken 43350 times.
44271 if (size_ <= kNumInit)
99 921 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 44271 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44271 times.
44271 assert(new_size <= capacity_);
114 44271 size_ = new_size;
115 44271 }
116
117 39401336 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 58633379 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 58633379 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 2151 times.
✓ Branch 1 taken 29319038 times.
58633379 if (num_bytes >= kMmapThreshold) {
128 2195 result = static_cast<Item *>(smmap(num_bytes));
129 2195 large_alloc_ = true;
130 } else {
131 58631184 result = static_cast<Item *>(smalloc(num_bytes));
132 58626504 large_alloc_ = false;
133 }
134 58628699 capacity_ = num_elements;
135 58628699 return result;
136 }
137
138 58701680 void Dealloc() {
139 58701680 FreeBuffer(buffer_, size_, large_alloc_);
140 58710550 buffer_ = NULL;
141 58710550 capacity_ = 0;
142 58710550 size_ = 0;
143 58710550 }
144
145 58710724 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 4758920591 times.
✓ Branch 1 taken 29358743 times.
4817883913 for (size_t i = 0; i < size; ++i)
147 4759173189 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 29359647 times.
✗ Branch 1 not taken.
58710724 if (buf) {
150
2/2
✓ Branch 0 taken 2004 times.
✓ Branch 1 taken 29357643 times.
58712532 if (large) {
151 2048 smunmap(buf);
152 } else {
153 58710484 free(buf);
154 }
155 }
156 58710724 }
157
158 19504687 void CopyFrom(const BigVector<Item> &other) {
159 19504687 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 980032097 times.
✓ Branch 1 taken 19505233 times.
999537330 for (size_t i = 0; i < other.size_; ++i) {
161 980032097 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 19505233 size_ = other.size_;
164 19505233 shared_buffer_ = false;
165 19505233 }
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