GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-02-22 02:35:58
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 4316 BigVector() {
19 4316 buffer_ = Alloc(kNumInit);
20 4316 size_ = 0;
21 4316 shared_buffer_ = false;
22 4316 }
23
24 6833220 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6833220 times.
6833220 assert(num_items > 0);
26 6833220 buffer_ = Alloc(num_items);
27 6833220 size_ = 0;
28 6833220 shared_buffer_ = false;
29 6833220 }
30
31 13503500 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 56 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
56 if (!shared_buffer_)
38 56 Dealloc();
39 56 CopyFrom(other);
40 56 return *this;
41 }
42
43 40652222 ~BigVector() {
44
1/2
✓ Branch 0 taken 20326418 times.
✗ Branch 1 not taken.
40652222 if (!shared_buffer_)
45 40652824 Dealloc();
46 40660902 }
47
48 60152789 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60131788 times.
60152789 assert(index < size_);
50 60152789 return buffer_[index];
51 }
52
53 40046886 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40023443 times.
40046886 assert(index < size_);
55 40046886 return &buffer_[index];
56 }
57
58 80088093 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 2868 times.
✓ Branch 1 taken 80077904 times.
80088093 if (size_ == capacity_)
60 2910 DoubleCapacity();
61
1/2
✓ Branch 2 taken 284 times.
✗ Branch 3 not taken.
80088093 new (buffer_ + size_) Item(item);
62 80088093 size_++;
63 80088093 }
64
65 9029 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9029 times.
9029 assert(index < size_);
67 9029 buffer_[index] = item;
68 9029 }
69
70 66 bool IsEmpty() const { return size_ == 0; }
71
72 10 void Clear() {
73 10 Dealloc();
74 10 buffer_ = Alloc(kNumInit);
75 10 }
76
77 2 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 2 *duplicate = buffer_;
79 2 *large_alloc = large_alloc_;
80 2 shared_buffer_ = true;
81 2 }
82
83 2910 void DoubleCapacity() {
84 2910 Item *old_buffer = buffer_;
85 2910 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2868 times.
2910 assert(capacity_ > 0);
88 2910 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 134293028 times.
✓ Branch 1 taken 2868 times.
134302994 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
134300084 new (buffer_ + i) Item(old_buffer[i]);
91
92 2910 FreeBuffer(old_buffer, size_, old_large_alloc);
93 2910 }
94
95 7033 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7033 times.
7033 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 148 times.
✓ Branch 1 taken 6885 times.
7033 if (size_ <= kNumInit)
99 148 return;
100
2/2
✓ Branch 0 taken 6855 times.
✓ Branch 1 taken 30 times.
6885 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 6855 return;
102
103 30 bool old_large_alloc = large_alloc_;
104 30 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 3532 times.
✓ Branch 1 taken 30 times.
3562 for (size_t i = 0; i < size_; ++i)
106 3532 new (new_buffer + i) Item(buffer_[i]);
107 30 FreeBuffer(buffer_, size_, old_large_alloc);
108 30 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 7033 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7033 times.
7033 assert(new_size <= capacity_);
114 7033 size_ = new_size;
115 7033 }
116
117 27068084 size_t size() const { return size_; }
118 32 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 40594854 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 40594854 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 93 times.
✓ Branch 1 taken 20298514 times.
40594854 if (num_bytes >= kMmapThreshold) {
128 100 result = static_cast<Item *>(smmap(num_bytes));
129 100 large_alloc_ = true;
130 } else {
131 40594754 result = static_cast<Item *>(smalloc(num_bytes));
132 40591730 large_alloc_ = false;
133 }
134 40591830 capacity_ = num_elements;
135 40591830 return result;
136 }
137
138 40652186 void Dealloc() {
139 40652186 FreeBuffer(buffer_, size_, large_alloc_);
140 40664012 buffer_ = NULL;
141 40664012 capacity_ = 0;
142 40664012 size_ = 0;
143 40664012 }
144
145 40657473 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 194390395 times.
✓ Branch 1 taken 20329121 times.
235201716 for (size_t i = 0; i < size; ++i)
147 194544243 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 20329921 times.
✗ Branch 1 not taken.
40657473 if (buf) {
150
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 20329834 times.
40659073 if (large) {
151 94 smunmap(buf);
152 } else {
153 40658979 free(buf);
154 }
155 }
156 40657473 }
157
158 13503529 void CopyFrom(const BigVector<Item> &other) {
159 13503529 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 40022221 times.
✓ Branch 1 taken 13503475 times.
53525696 for (size_t i = 0; i < other.size_; ++i) {
161 40022221 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 13503475 size_ = other.size_;
164 13503475 shared_buffer_ = false;
165 13503475 }
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