GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2025-06-29 02:35:41
Exec Total Coverage
Lines: 105 106 99.1%
Branches: 31 44 70.5%

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 5792 BigVector() {
19 5792 buffer_ = Alloc(kNumInit);
20 5792 size_ = 0;
21 5792 shared_buffer_ = false;
22 5792 }
23
24 9844372 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9844372 times.
9844372 assert(num_items > 0);
26 9844372 buffer_ = Alloc(num_items);
27 9844372 size_ = 0;
28 9844372 shared_buffer_ = false;
29 9844372 }
30
31 19502826 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 94 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 94 times.
94 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 94 times.
✗ Branch 1 not taken.
94 if (!shared_buffer_)
38 94 Dealloc();
39 94 CopyFrom(other);
40 94 return *this;
41 }
42
43 58670214 ~BigVector() {
44
1/2
✓ Branch 0 taken 29335454 times.
✗ Branch 1 not taken.
58670214 if (!shared_buffer_)
45 58670662 Dealloc();
46 58677268 }
47
48 480487204 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 480355247 times.
480487204 assert(index < size_);
50 480487204 return buffer_[index];
51 }
52
53 320067898 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 320033949 times.
320067898 assert(index < size_);
55 320067898 return &buffer_[index];
56 }
57
58 640316681 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 5455 times.
✓ Branch 1 taken 640266709 times.
640316681 if (size_ == capacity_)
60 5719 DoubleCapacity();
61
1/2
✓ Branch 2 taken 295 times.
✗ Branch 3 not taken.
640316681 new (buffer_ + size_) Item(item);
62 640316681 size_++;
63 640316681 }
64
65 60177 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60177 times.
60177 assert(index < size_);
67 60177 buffer_[index] = item;
68 60177 }
69
70 33 bool IsEmpty() const { return size_ == 0; }
71
72 170 void Clear() {
73 170 Dealloc();
74 170 buffer_ = Alloc(kNumInit);
75 170 }
76
77 16 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 16 *duplicate = buffer_;
79 16 *large_alloc = large_alloc_;
80 16 shared_buffer_ = true;
81 16 }
82
83 5719 void DoubleCapacity() {
84 5719 Item *old_buffer = buffer_;
85 5719 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5455 times.
5719 assert(capacity_ > 0);
88 5719 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 1074007921 times.
✓ Branch 1 taken 5455 times.
1074057992 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1074052273 new (buffer_ + i) Item(old_buffer[i]);
91
92 5719 FreeBuffer(old_buffer, size_, old_large_alloc);
93 5719 }
94
95 44209 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44209 times.
44209 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 925 times.
✓ Branch 1 taken 43284 times.
44209 if (size_ <= kNumInit)
99 925 return;
100
2/2
✓ Branch 0 taken 43092 times.
✓ Branch 1 taken 192 times.
43284 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 43092 return;
102
103 192 bool old_large_alloc = large_alloc_;
104 192 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 22544 times.
✓ Branch 1 taken 192 times.
22736 for (size_t i = 0; i < size_; ++i)
106 22544 new (new_buffer + i) Item(buffer_[i]);
107 192 FreeBuffer(buffer_, size_, old_large_alloc);
108 192 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 44209 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44209 times.
44209 assert(new_size <= capacity_);
114 44209 size_ = new_size;
115 44209 }
116
117 39414742 size_t size() const { return size_; }
118 256 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 58629195 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 58629195 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 732 times.
✓ Branch 1 taken 29316379 times.
58629195 if (num_bytes >= kMmapThreshold) {
128 776 result = static_cast<Item *>(smmap(num_bytes));
129 776 large_alloc_ = true;
130 } else {
131 58628419 result = static_cast<Item *>(smalloc(num_bytes));
132 58621945 large_alloc_ = false;
133 }
134 58622721 capacity_ = num_elements;
135 58622721 return result;
136 }
137
138 58669682 void Dealloc() {
139 58669682 FreeBuffer(buffer_, size_, large_alloc_);
140 58683660 buffer_ = NULL;
141 58683660 capacity_ = 0;
142 58683660 size_ = 0;
143 58683660 }
144
145 58678352 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 1554274365 times.
✓ Branch 1 taken 29341088 times.
1613154934 for (size_t i = 0; i < size; ++i)
147 1554476582 buf[i].~Item();
148
149
2/2
✓ Branch 0 taken 29340787 times.
✓ Branch 1 taken 301 times.
58678352 if (buf) {
150
2/2
✓ Branch 0 taken 684 times.
✓ Branch 1 taken 29340103 times.
58677750 if (large) {
151 728 smunmap(buf);
152 } else {
153 58677022 free(buf);
154 }
155 }
156 58678352 }
157
158 19503037 void CopyFrom(const BigVector<Item> &other) {
159 19503037 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 320032097 times.
✓ Branch 1 taken 19502842 times.
339534939 for (size_t i = 0; i < other.size_; ++i) {
161 320032097 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 19502842 size_ = other.size_;
164 19502842 shared_buffer_ = false;
165 19502842 }
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