GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-07-19 02:35:15
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 4954 BigVector() {
19 4954 buffer_ = Alloc(kNumInit);
20 4954 size_ = 0;
21 4954 shared_buffer_ = false;
22 4954 }
23
24 1312784 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1312784 times.
1312784 assert(num_items > 0);
26 1312784 buffer_ = Alloc(num_items);
27 1312784 size_ = 0;
28 1312784 shared_buffer_ = false;
29 1312784 }
30
31 2503868 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 11 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (!shared_buffer_)
38 11 Dealloc();
39 11 CopyFrom(other);
40 11 return *this;
41 }
42
43 7631654 ~BigVector() {
44
1/2
✓ Branch 0 taken 3816021 times.
✗ Branch 1 not taken.
7631654 if (!shared_buffer_)
45 7631911 Dealloc();
46 7632786 }
47
48 30400122 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30262125 times.
30400122 assert(index < size_);
50 30400122 return buffer_[index];
51 }
52
53 20009580 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20004790 times.
20009580 assert(index < size_);
55 20009580 return &buffer_[index];
56 }
57
58 40206093 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 1832 times.
✓ Branch 1 taken 40157660 times.
40206093 if (size_ == capacity_)
60 2108 DoubleCapacity();
61
1/2
✓ Branch 2 taken 375 times.
✗ Branch 3 not taken.
40206093 new (buffer_ + size_) Item(item);
62 40206093 size_++;
63 40206093 }
64
65 47181 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47181 times.
47181 assert(index < size_);
67 47181 buffer_[index] = item;
68 47181 }
69
70 51 bool IsEmpty() const { return size_ == 0; }
71
72 80 void Clear() {
73 80 Dealloc();
74 80 buffer_ = Alloc(kNumInit);
75 80 }
76
77 1 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 1 *duplicate = buffer_;
79 1 *large_alloc = large_alloc_;
80 1 shared_buffer_ = true;
81 1 }
82
83 2108 void DoubleCapacity() {
84 2108 Item *old_buffer = buffer_;
85 2108 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1832 times.
2108 assert(capacity_ > 0);
88 2108 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 67262772 times.
✓ Branch 1 taken 1832 times.
67311248 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
67309140 new (buffer_ + i) Item(old_buffer[i]);
91
92 2108 FreeBuffer(old_buffer, size_, old_large_alloc);
93 2108 }
94
95 46183 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46183 times.
46183 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 963 times.
✓ Branch 1 taken 45220 times.
46183 if (size_ <= kNumInit)
99 963 return;
100
2/2
✓ Branch 0 taken 45035 times.
✓ Branch 1 taken 185 times.
45220 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 45035 return;
102
103 185 bool const old_large_alloc = large_alloc_;
104 185 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 21996 times.
✓ Branch 1 taken 185 times.
22181 for (size_t i = 0; i < size_; ++i)
106 21996 new (new_buffer + i) Item(buffer_[i]);
107 185 FreeBuffer(buffer_, size_, old_large_alloc);
108 185 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 46183 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46183 times.
46183 assert(new_size <= capacity_);
114 46183 size_ = new_size;
115 46183 }
116
117 5406870 size_t size() const { return size_; }
118 16 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 7621354 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 7621354 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 3811892 times.
7621354 if (num_bytes >= kMmapThreshold) {
128 135 result = static_cast<Item *>(smmap(num_bytes));
129 135 large_alloc_ = true;
130 } else {
131 7621219 result = static_cast<Item *>(smalloc(num_bytes));
132 7620679 large_alloc_ = false;
133 }
134 7620814 capacity_ = num_elements;
135 7620814 return result;
136 }
137
138 7631536 void Dealloc() {
139 7631536 FreeBuffer(buffer_, size_, large_alloc_);
140 7634954 buffer_ = NULL;
141 7634954 capacity_ = 0;
142 7634954 size_ = 0;
143 7634954 }
144
145 7633961 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 97401190 times.
✓ Branch 1 taken 3817856 times.
105164951 for (size_t i = 0; i < size; ++i)
147 97530990 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 3818060 times.
✗ Branch 1 not taken.
7633961 if (buf) {
150
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 3817974 times.
7634369 if (large) {
151 132 smunmap(buf);
152 } else {
153 7634237 free(buf);
154 }
155 }
156 7633961 }
157
158 2503894 void CopyFrom(const BigVector<Item> &other) {
159 2503894 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 20004115 times.
✓ Branch 1 taken 2504064 times.
22508179 for (size_t i = 0; i < other.size_; ++i) {
161 20004115 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 2504064 size_ = other.size_;
164 2504064 shared_buffer_ = false;
165 2504064 }
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