GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2025-10-19 02:35:28
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 7027 BigVector() {
19 7027 buffer_ = Alloc(kNumInit);
20 7027 size_ = 0;
21 7027 shared_buffer_ = false;
22 7027 }
23
24 1075583 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1075583 times.
1075583 assert(num_items > 0);
26 1075583 buffer_ = Alloc(num_items);
27 1075583 size_ = 0;
28 1075583 shared_buffer_ = false;
29 1075583 }
30
31 2003941 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 12 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (!shared_buffer_)
38 12 Dealloc();
39 12 CopyFrom(other);
40 12 return *this;
41 }
42
43 6161094 ~BigVector() {
44
1/2
✓ Branch 0 taken 3080866 times.
✗ Branch 1 not taken.
6161094 if (!shared_buffer_)
45 6161528 Dealloc();
46 6162110 }
47
48 120294600 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 120291602 times.
120294600 assert(index < size_);
50 120294600 return buffer_[index];
51 }
52
53 80009896 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 80004948 times.
80009896 assert(index < size_);
55 80009896 return &buffer_[index];
56 }
57
58 160225999 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 3510 times.
✓ Branch 1 taken 160221106 times.
160225999 if (size_ == capacity_)
60 3516 DoubleCapacity();
61
1/2
✓ Branch 2 taken 376 times.
✗ Branch 3 not taken.
160225999 new (buffer_ + size_) Item(item);
62 160225999 size_++;
63 160225999 }
64
65 5005 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5005 times.
5005 assert(index < size_);
67 5005 buffer_[index] = item;
68 5005 }
69
70 114 bool IsEmpty() const { return size_ == 0; }
71
72 128 void Clear() {
73 128 Dealloc();
74 128 buffer_ = Alloc(kNumInit);
75 128 }
76
77 4 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 4 *duplicate = buffer_;
79 4 *large_alloc = large_alloc_;
80 4 shared_buffer_ = true;
81 4 }
82
83 3516 void DoubleCapacity() {
84 3516 Item *old_buffer = buffer_;
85 3516 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3510 times.
3516 assert(capacity_ > 0);
88 3516 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 268645541 times.
✓ Branch 1 taken 3510 times.
268650065 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
268646549 new (buffer_ + i) Item(old_buffer[i]);
91
92 3516 FreeBuffer(old_buffer, size_, old_large_alloc);
93 3516 }
94
95 1013 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1013 times.
1013 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 991 times.
1013 if (size_ <= kNumInit)
99 22 return;
100
2/2
✓ Branch 0 taken 983 times.
✓ Branch 1 taken 8 times.
991 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 983 return;
102
103 8 bool old_large_alloc = large_alloc_;
104 8 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 876 times.
✓ Branch 1 taken 8 times.
884 for (size_t i = 0; i < size_; ++i)
106 876 new (new_buffer + i) Item(buffer_[i]);
107 8 FreeBuffer(buffer_, size_, old_large_alloc);
108 8 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 1013 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1013 times.
1013 assert(new_size <= capacity_);
114 1013 size_ = new_size;
115 1013 }
116
117 4061046 size_t size() const { return size_; }
118 64 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 6161731 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 6161731 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 3083216 times.
6161731 if (num_bytes >= kMmapThreshold) {
128 174 result = static_cast<Item *>(smmap(num_bytes));
129 174 large_alloc_ = true;
130 } else {
131 6161557 result = static_cast<Item *>(smalloc(num_bytes));
132 6161109 large_alloc_ = false;
133 }
134 6161283 capacity_ = num_elements;
135 6161283 return result;
136 }
137
138 6161236 void Dealloc() {
139 6161236 FreeBuffer(buffer_, size_, large_alloc_);
140 6163340 buffer_ = NULL;
141 6163340 capacity_ = 0;
142 6163340 size_ = 0;
143 6163340 }
144
145 6166146 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 388869306 times.
✓ Branch 1 taken 3084201 times.
395212426 for (size_t i = 0; i < size; ++i)
147 389046280 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 3084228 times.
✗ Branch 1 not taken.
6166146 if (buf) {
150
2/2
✓ Branch 0 taken 161 times.
✓ Branch 1 taken 3084067 times.
6166200 if (large) {
151 162 smunmap(buf);
152 } else {
153 6166038 free(buf);
154 }
155 }
156 6166146 }
157
158 2003981 void CopyFrom(const BigVector<Item> &other) {
159 2003981 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 80003292 times.
✓ Branch 1 taken 2004053 times.
82007345 for (size_t i = 0; i < other.size_; ++i) {
161 80003292 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 2004053 size_ = other.size_;
164 2004053 shared_buffer_ = false;
165 2004053 }
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