GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2026-10-04 02:40:33
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 3794 BigVector() {
19 3794 buffer_ = Alloc(kNumInit);
20 3794 size_ = 0;
21 3794 shared_buffer_ = false;
22 3794 }
23
24 3058731 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3058731 times.
3058731 assert(num_items > 0);
26 3058731 buffer_ = Alloc(num_items);
27 3058731 size_ = 0;
28 3058731 shared_buffer_ = false;
29 3058731 }
30
31 6003164 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 40 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (&other == this)
35 ✗ return *this;
36
37
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (!shared_buffer_)
38 40 Dealloc();
39 40 CopyFrom(other);
40 40 return *this;
41 }
42
43 18108949 ~BigVector() {
44
1/2
✓ Branch 0 taken 9054825 times.
✗ Branch 1 not taken.
18108949 if (!shared_buffer_)
45 18109569 Dealloc();
46 18118151 }
47
48 480198386 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 480144519 times.
480198386 assert(index < size_);
50 480198386 return buffer_[index];
51 }
52
53 320024190 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 320012095 times.
320024190 assert(index < size_);
55 320024190 return &buffer_[index];
56 }
57
58 640126829 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 2731 times.
✓ Branch 1 taken 640105700 times.
640126829 if (size_ == capacity_)
60 2839 DoubleCapacity();
61
1/2
✓ Branch 2 taken 314 times.
✗ Branch 3 not taken.
640126829 new (buffer_ + size_) Item(item);
62 640126829 size_++;
63 640126829 }
64
65 34067 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34067 times.
34067 assert(index < size_);
67 34067 buffer_[index] = item;
68 34067 }
69
70 36 bool IsEmpty() const { return size_ == 0; }
71
72 71 void Clear() {
73 71 Dealloc();
74 71 buffer_ = Alloc(kNumInit);
75 71 }
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 2839 void DoubleCapacity() {
84 2839 Item *old_buffer = buffer_;
85 2839 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2731 times.
2839 assert(capacity_ > 0);
88 2839 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 1073847068 times.
✓ Branch 1 taken 2731 times.
1073868051 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1073865212 new (buffer_ + i) Item(old_buffer[i]);
91
92 2839 FreeBuffer(old_buffer, size_, old_large_alloc);
93 2839 }
94
95 18099 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18099 times.
18099 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 373 times.
✓ Branch 1 taken 17726 times.
18099 if (size_ <= kNumInit)
99 373 return;
100
2/2
✓ Branch 0 taken 17638 times.
✓ Branch 1 taken 88 times.
17726 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 17638 return;
102
103 88 bool const old_large_alloc = large_alloc_;
104 88 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 10168 times.
✓ Branch 1 taken 88 times.
10256 for (size_t i = 0; i < size_; ++i)
106 10168 new (new_buffer + i) Item(buffer_[i]);
107 88 FreeBuffer(buffer_, size_, old_large_alloc);
108 88 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 18099 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18099 times.
18099 assert(new_size <= capacity_);
114 18099 size_ = new_size;
115 18099 }
116
117 12167425 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 9066551 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 9066551 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 706 times.
✓ Branch 1 taken 9065398 times.
9066551 if (num_bytes >= kMmapThreshold) {
128 724 result = static_cast<Item *>(smmap(num_bytes));
129 724 large_alloc_ = true;
130 } else {
131 9065827 result = static_cast<Item *>(smalloc(num_bytes));
132 9065011 large_alloc_ = false;
133 }
134 9065735 capacity_ = num_elements;
135 9065735 return result;
136 }
137
138 18108908 void Dealloc() {
139 18108908 FreeBuffer(buffer_, size_, large_alloc_);
140 18118470 buffer_ = NULL;
141 18118470 capacity_ = 0;
142 18118470 size_ = 0;
143 18118470 }
144
145 9057791 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 1553941472 times.
✓ Branch 1 taken 9056982 times.
1563048145 for (size_t i = 0; i < size; ++i)
147 1553990354 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 9057175 times.
✗ Branch 1 not taken.
9057791 if (buf) {
150
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 9056517 times.
9057984 if (large) {
151 676 smunmap(buf);
152 } else {
153 9057308 free(buf);
154 }
155 }
156 9057791 }
157
158 6003312 void CopyFrom(const BigVector<Item> &other) {
159 6003312 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 320009876 times.
✓ Branch 1 taken 6002940 times.
326012816 for (size_t i = 0; i < other.size_; ++i) {
161 320009876 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 6002940 size_ = other.size_;
164 6002940 shared_buffer_ = false;
165 6002940 }
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