GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-03-15 02:35:27
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 8883 BigVector() {
19 8883 buffer_ = Alloc(kNumInit);
20 8883 size_ = 0;
21 8883 shared_buffer_ = false;
22 8883 }
23
24 10382597 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10382597 times.
10382597 assert(num_items > 0);
26 10382597 buffer_ = Alloc(num_items);
27 10382597 size_ = 0;
28 10382597 shared_buffer_ = false;
29 10382597 }
30
31 20504725 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 83 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83 times.
83 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 83 times.
✗ Branch 1 not taken.
83 if (!shared_buffer_)
38 83 Dealloc();
39 83 CopyFrom(other);
40 83 return *this;
41 }
42
43 61738363 ~BigVector() {
44
1/2
✓ Branch 0 taken 30870027 times.
✗ Branch 1 not taken.
61738363 if (!shared_buffer_)
45 61739858 Dealloc();
46 61753215 }
47
48 30568632 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30427815 times.
30568632 assert(index < size_);
50 30568632 return buffer_[index];
51 }
52
53 20070872 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20035436 times.
20070872 assert(index < size_);
55 20070872 return &buffer_[index];
56 }
57
58 40328481 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 5341 times.
✓ Branch 1 taken 40275622 times.
40328481 if (size_ == capacity_)
60 5623 DoubleCapacity();
61
1/2
✓ Branch 2 taken 279 times.
✗ Branch 3 not taken.
40328481 new (buffer_ + size_) Item(item);
62 40328481 size_++;
63 40328481 }
64
65 48191 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48191 times.
48191 assert(index < size_);
67 48191 buffer_[index] = item;
68 48191 }
69
70 27 bool IsEmpty() const { return size_ == 0; }
71
72 119 void Clear() {
73 119 Dealloc();
74 119 buffer_ = Alloc(kNumInit);
75 119 }
76
77 1 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 1 *duplicate = buffer_;
79 1 *large_alloc = large_alloc_;
80 1 shared_buffer_ = true;
81 1 }
82
83 5623 void DoubleCapacity() {
84 5623 Item *old_buffer = buffer_;
85 5623 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5341 times.
5623 assert(capacity_ > 0);
88 5623 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 67377547 times.
✓ Branch 1 taken 5341 times.
67430546 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
67424923 new (buffer_ + i) Item(old_buffer[i]);
91
92 5623 FreeBuffer(old_buffer, size_, old_large_alloc);
93 5623 }
94
95 47193 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47193 times.
47193 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 990 times.
✓ Branch 1 taken 46203 times.
47193 if (size_ <= kNumInit)
99 990 return;
100
2/2
✓ Branch 0 taken 46014 times.
✓ Branch 1 taken 189 times.
46203 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 46014 return;
102
103 189 bool old_large_alloc = large_alloc_;
104 189 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 22472 times.
✓ Branch 1 taken 189 times.
22661 for (size_t i = 0; i < size_; ++i)
106 22472 new (new_buffer + i) Item(buffer_[i]);
107 189 FreeBuffer(buffer_, size_, old_large_alloc);
108 189 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 47193 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47193 times.
47193 assert(new_size <= capacity_);
114 47193 size_ = new_size;
115 47193 }
116
117 41435141 size_t size() const { return size_; }
118 16 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 61662620 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 61662620 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 30833986 times.
61662620 if (num_bytes >= kMmapThreshold) {
128 137 result = static_cast<Item *>(smmap(num_bytes));
129 137 large_alloc_ = true;
130 } else {
131 61662483 result = static_cast<Item *>(smalloc(num_bytes));
132 61655677 large_alloc_ = false;
133 }
134 61655814 capacity_ = num_elements;
135 61655814 return result;
136 }
137
138 61738176 void Dealloc() {
139 61738176 FreeBuffer(buffer_, size_, large_alloc_);
140 61758758 buffer_ = NULL;
141 61758758 capacity_ = 0;
142 61758758 size_ = 0;
143 61758758 }
144
145 61747405 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 97666340 times.
✓ Branch 1 taken 30875032 times.
159728877 for (size_t i = 0; i < size; ++i)
147 97981472 buf[i].~Item();
148
149
2/2
✓ Branch 0 taken 30874860 times.
✓ Branch 1 taken 172 times.
61747405 if (buf) {
150
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 30874773 times.
61747061 if (large) {
151 134 smunmap(buf);
152 } else {
153 61746927 free(buf);
154 }
155 }
156 61747405 }
157
158 20504890 void CopyFrom(const BigVector<Item> &other) {
159 20504890 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 20033743 times.
✓ Branch 1 taken 20505751 times.
40539494 for (size_t i = 0; i < other.size_; ++i) {
161 20033743 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 20505751 size_ = other.size_;
164 20505751 shared_buffer_ = false;
165 20505751 }
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