GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-11-08 02:41:18
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 4352 BigVector() {
19 4352 buffer_ = Alloc(kNumInit);
20 4352 size_ = 0;
21 4352 shared_buffer_ = false;
22 4352 }
23
24 1540302 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1540302 times.
1540302 assert(num_items > 0);
26 1540302 buffer_ = Alloc(num_items);
27 1540302 size_ = 0;
28 1540302 shared_buffer_ = false;
29 1540302 }
30
31 3002108 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 44 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 if (&other == this)
35 ✗ return *this;
36
37
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (!shared_buffer_)
38 44 Dealloc();
39 44 CopyFrom(other);
40 44 return *this;
41 }
42
43 9076795 ~BigVector() {
44
1/2
✓ Branch 0 taken 4538658 times.
✗ Branch 1 not taken.
9076795 if (!shared_buffer_)
45 9077039 Dealloc();
46 9084343 }
47
48 960356145 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 960269094 times.
960356145 assert(index < size_);
50 960356145 return buffer_[index];
51 }
52
53 640011672 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 640005836 times.
640011672 assert(index < size_);
55 640011672 return &buffer_[index];
56 }
57
58 1280214149 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 4596 times.
✓ Branch 1 taken 1280180158 times.
1280214149 if (size_ == capacity_)
60 4770 DoubleCapacity();
61
1/2
✓ Branch 2 taken 256 times.
✗ Branch 3 not taken.
1280214149 new (buffer_ + size_) Item(item);
62 1280214149 size_++;
63 1280214149 }
64
65 61111 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61111 times.
61111 assert(index < size_);
67 61111 buffer_[index] = item;
68 61111 }
69
70 177 bool IsEmpty() const { return size_ == 0; }
71
72 115 void Clear() {
73 115 Dealloc();
74 115 buffer_ = Alloc(kNumInit);
75 115 }
76
77 32 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 32 *duplicate = buffer_;
79 32 *large_alloc = large_alloc_;
80 32 shared_buffer_ = true;
81 32 }
82
83 4770 void DoubleCapacity() {
84 4770 Item *old_buffer = buffer_;
85 4770 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4596 times.
4770 assert(capacity_ > 0);
88 4770 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 2147660889 times.
✓ Branch 1 taken 4596 times.
2147694891 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2147690121 new (buffer_ + i) Item(old_buffer[i]);
91
92 4770 FreeBuffer(old_buffer, size_, old_large_alloc);
93 4770 }
94
95 29175 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29175 times.
29175 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 604 times.
✓ Branch 1 taken 28571 times.
29175 if (size_ <= kNumInit)
99 604 return;
100
2/2
✓ Branch 0 taken 28423 times.
✓ Branch 1 taken 148 times.
28571 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 28423 return;
102
103 148 bool const old_large_alloc = large_alloc_;
104 148 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 17004 times.
✓ Branch 1 taken 148 times.
17152 for (size_t i = 0; i < size_; ++i)
106 17004 new (new_buffer + i) Item(buffer_[i]);
107 148 FreeBuffer(buffer_, size_, old_large_alloc);
108 148 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 29175 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29175 times.
29175 assert(new_size <= capacity_);
114 29175 size_ = new_size;
115 29175 }
116
117 6256747 size_t size() const { return size_; }
118 512 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 4550208 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 4550208 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 1405 times.
✓ Branch 1 taken 4548030 times.
4550208 if (num_bytes >= kMmapThreshold) {
128 1434 result = static_cast<Item *>(smmap(num_bytes));
129 1434 large_alloc_ = true;
130 } else {
131 4548774 result = static_cast<Item *>(smalloc(num_bytes));
132 4548762 large_alloc_ = false;
133 }
134 4550196 capacity_ = num_elements;
135 4550196 return result;
136 }
137
138 9077090 void Dealloc() {
139 9077090 FreeBuffer(buffer_, size_, large_alloc_);
140 9084932 buffer_ = NULL;
141 9084932 capacity_ = 0;
142 9084932 size_ = 0;
143 9084932 }
144
145 4545303 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 3427841645 times.
✓ Branch 1 taken 4543432 times.
3432496267 for (size_t i = 0; i < size; ++i)
147 3427950964 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 4543643 times.
✗ Branch 1 not taken.
4545303 if (buf) {
150
2/2
✓ Branch 0 taken 1373 times.
✓ Branch 1 taken 4542270 times.
4545514 if (large) {
151 1402 smunmap(buf);
152 } else {
153 4544112 free(buf);
154 }
155 }
156 4545303 }
157
158 3002140 void CopyFrom(const BigVector<Item> &other) {
159 3002140 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 640004938 times.
✓ Branch 1 taken 3002212 times.
643007150 for (size_t i = 0; i < other.size_; ++i) {
161 640004938 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 3002212 size_ = other.size_;
164 3002212 shared_buffer_ = false;
165 3002212 }
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