GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-05-19 11:45:12
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 3860 BigVector() {
19 3860 buffer_ = Alloc(kNumInit);
20 3860 size_ = 0;
21 3860 shared_buffer_ = false;
22 3860 }
23
24 7098030 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7098030 times.
7098030 assert(num_items > 0);
26 7098030 buffer_ = Alloc(num_items);
27 7098030 size_ = 0;
28 7098030 shared_buffer_ = false;
29 7098030 }
30
31 14003121 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 82 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 82 times.
82 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 82 times.
✗ Branch 1 not taken.
82 if (!shared_buffer_)
38 82 Dealloc();
39 82 CopyFrom(other);
40 82 return *this;
41 }
42
43 42163113 ~BigVector() {
44
1/2
✓ Branch 0 taken 21082311 times.
✗ Branch 1 not taken.
42163113 if (!shared_buffer_)
45 42164431 Dealloc();
46 42175937 }
47
48 780217528 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 780205539 times.
780217528 assert(index < size_);
50 780217528 return buffer_[index];
51 }
52
53 520047184 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 520023592 times.
520047184 assert(index < size_);
55 520047184 return &buffer_[index];
56 }
57
58 1040202556 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 5204 times.
✓ Branch 1 taken 1040192999 times.
1040202556 if (size_ == capacity_)
60 5228 DoubleCapacity();
61
1/2
✓ Branch 2 taken 331 times.
✗ Branch 3 not taken.
1040202556 new (buffer_ + size_) Item(item);
62 1040202556 size_++;
63 1040202556 }
64
65 30017 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30017 times.
30017 assert(index < size_);
67 30017 buffer_[index] = item;
68 30017 }
69
70 99 bool IsEmpty() const { return size_ == 0; }
71
72 151 void Clear() {
73 151 Dealloc();
74 151 buffer_ = Alloc(kNumInit);
75 151 }
76
77 26 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 26 *duplicate = buffer_;
79 26 *large_alloc = large_alloc_;
80 26 shared_buffer_ = true;
81 26 }
82
83 5228 void DoubleCapacity() {
84 5228 Item *old_buffer = buffer_;
85 5228 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5204 times.
5228 assert(capacity_ > 0);
88 5228 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 1745023883 times.
✓ Branch 1 taken 5204 times.
1745033143 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1745027915 new (buffer_ + i) Item(old_buffer[i]);
91
92 5228 FreeBuffer(old_buffer, size_, old_large_alloc);
93 5228 }
94
95 4069 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4069 times.
4069 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 3984 times.
4069 if (size_ <= kNumInit)
99 85 return;
100
2/2
✓ Branch 0 taken 3942 times.
✓ Branch 1 taken 42 times.
3984 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 3942 return;
102
103 42 bool old_large_alloc = large_alloc_;
104 42 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 4504 times.
✓ Branch 1 taken 42 times.
4546 for (size_t i = 0; i < size_; ++i)
106 4504 new (new_buffer + i) Item(buffer_[i]);
107 42 FreeBuffer(buffer_, size_, old_large_alloc);
108 42 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 4069 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4069 times.
4069 assert(new_size <= capacity_);
114 4069 size_ = new_size;
115 4069 }
116
117 28077341 size_t size() const { return size_; }
118 416 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 42120217 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 42120217 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 1122 times.
✓ Branch 1 taken 21061364 times.
42120217 if (num_bytes >= kMmapThreshold) {
128 1126 result = static_cast<Item *>(smmap(num_bytes));
129 1126 large_alloc_ = true;
130 } else {
131 42119091 result = static_cast<Item *>(smalloc(num_bytes));
132 42112931 large_alloc_ = false;
133 }
134 42114057 capacity_ = num_elements;
135 42114057 return result;
136 }
137
138 42163252 void Dealloc() {
139 42163252 FreeBuffer(buffer_, size_, large_alloc_);
140 42182880 buffer_ = NULL;
141 42182880 capacity_ = 0;
142 42182880 size_ = 0;
143 42182880 }
144
145 42170794 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 2525219613 times.
✓ Branch 1 taken 21087273 times.
2567498664 for (size_t i = 0; i < size; ++i)
147 2525327870 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 21088085 times.
✗ Branch 1 not taken.
42170794 if (buf) {
150
2/2
✓ Branch 0 taken 1044 times.
✓ Branch 1 taken 21087041 times.
42172418 if (large) {
151 1048 smunmap(buf);
152 } else {
153 42171370 free(buf);
154 }
155 }
156 42170794 }
157
158 14003091 void CopyFrom(const BigVector<Item> &other) {
159 14003091 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 520023044 times.
✓ Branch 1 taken 14003231 times.
534026275 for (size_t i = 0; i < other.size_; ++i) {
161 520023044 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 14003231 size_ = other.size_;
164 14003231 shared_buffer_ = false;
165 14003231 }
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