GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2025-12-21 02:39:23
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 6306 BigVector() {
19 6306 buffer_ = Alloc(kNumInit);
20 6306 size_ = 0;
21 6306 shared_buffer_ = false;
22 6306 }
23
24 9861388 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9861388 times.
9861388 assert(num_items > 0);
26 9861388 buffer_ = Alloc(num_items);
27 9861388 size_ = 0;
28 9861388 shared_buffer_ = false;
29 9861388 }
30
31 19504046 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 120 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120 times.
120 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
120 if (!shared_buffer_)
38 120 Dealloc();
39 120 CopyFrom(other);
40 120 return *this;
41 }
42
43 58716398 ~BigVector() {
44
1/2
✓ Branch 0 taken 29358605 times.
✗ Branch 1 not taken.
58716398 if (!shared_buffer_)
45 58717058 Dealloc();
46 58720592 }
47
48 1260312787 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1260303796 times.
1260312787 assert(index < size_);
50 1260312787 return buffer_[index];
51 }
52
53 840067694 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 840033847 times.
840067694 assert(index < size_);
55 840067694 return &buffer_[index];
56 }
57
58 1680251378 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 8073 times.
✓ Branch 1 taken 1680240061 times.
1680251378 if (size_ == capacity_)
60 8091 DoubleCapacity();
61
1/2
✓ Branch 2 taken 233 times.
✗ Branch 3 not taken.
1680251378 new (buffer_ + size_) Item(item);
62 1680251378 size_++;
63 1680251378 }
64
65 45009 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45009 times.
45009 assert(index < size_);
67 45009 buffer_[index] = item;
68 45009 }
69
70 117 bool IsEmpty() const { return size_ == 0; }
71
72 150 void Clear() {
73 150 Dealloc();
74 150 buffer_ = Alloc(kNumInit);
75 150 }
76
77 42 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 42 *duplicate = buffer_;
79 42 *large_alloc = large_alloc_;
80 42 shared_buffer_ = true;
81 42 }
82
83 8091 void DoubleCapacity() {
84 8091 Item *old_buffer = buffer_;
85 8091 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8073 times.
8091 assert(capacity_ > 0);
88 8091 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 2818809605 times.
✓ Branch 1 taken 8073 times.
2818820720 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2818812629 new (buffer_ + i) Item(old_buffer[i]);
91
92 8091 FreeBuffer(old_buffer, size_, old_large_alloc);
93 8091 }
94
95 3093 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3093 times.
3093 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 3033 times.
3093 if (size_ <= kNumInit)
99 60 return;
100
2/2
✓ Branch 0 taken 2979 times.
✓ Branch 1 taken 54 times.
3033 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 2979 return;
102
103 54 bool old_large_alloc = large_alloc_;
104 54 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 5628 times.
✓ Branch 1 taken 54 times.
5682 for (size_t i = 0; i < size_; ++i)
106 5628 new (new_buffer + i) Item(buffer_[i]);
107 54 FreeBuffer(buffer_, size_, old_large_alloc);
108 54 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 3093 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3093 times.
3093 assert(new_size <= capacity_);
114 3093 size_ = new_size;
115 3093 }
116
117 39060150 size_t size() const { return size_; }
118 672 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 58684508 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 58684508 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 1809 times.
✓ Branch 1 taken 29344409 times.
58684508 if (num_bytes >= kMmapThreshold) {
128 1812 result = static_cast<Item *>(smmap(num_bytes));
129 1812 large_alloc_ = true;
130 } else {
131 58682696 result = static_cast<Item *>(smalloc(num_bytes));
132 58679264 large_alloc_ = false;
133 }
134 58681076 capacity_ = num_elements;
135 58681076 return result;
136 }
137
138 58715080 void Dealloc() {
139 58715080 FreeBuffer(buffer_, size_, large_alloc_);
140 58726088 buffer_ = NULL;
141 58726088 capacity_ = 0;
142 58726088 size_ = 0;
143 58726088 }
144
145 58726221 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 4079050276 times.
✓ Branch 1 taken 29365779 times.
4137991498 for (size_t i = 0; i < size; ++i)
147 4079265277 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 29366520 times.
✗ Branch 1 not taken.
58726221 if (buf) {
150
2/2
✓ Branch 0 taken 1683 times.
✓ Branch 1 taken 29364837 times.
58727703 if (large) {
151 1686 smunmap(buf);
152 } else {
153 58726017 free(buf);
154 }
155 }
156 58726221 }
157
158 19504127 void CopyFrom(const BigVector<Item> &other) {
159 19504127 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 840032097 times.
✓ Branch 1 taken 19504556 times.
859536653 for (size_t i = 0; i < other.size_; ++i) {
161 840032097 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 19504556 size_ = other.size_;
164 19504556 shared_buffer_ = false;
165 19504556 }
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