GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigvector.h
Date: 2025-08-31 02:39:21
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 4785 BigVector() {
19 4785 buffer_ = Alloc(kNumInit);
20 4785 size_ = 0;
21 4785 shared_buffer_ = false;
22 4785 }
23
24 4571508 explicit BigVector(const size_t num_items) {
25
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4571508 times.
4571508 assert(num_items > 0);
26 4571508 buffer_ = Alloc(num_items);
27 4571508 size_ = 0;
28 4571508 shared_buffer_ = false;
29 4571508 }
30
31 9003838 BigVector(const BigVector<Item> &other) { CopyFrom(other); }
32
33 85 BigVector<Item> &operator=(const BigVector<Item> &other) {
34
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 85 times.
85 if (&other == this)
35 return *this;
36
37
1/2
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
85 if (!shared_buffer_)
38 85 Dealloc();
39 85 CopyFrom(other);
40 85 return *this;
41 }
42
43 27142688 ~BigVector() {
44
1/2
✓ Branch 0 taken 13571518 times.
✗ Branch 1 not taken.
27142688 if (!shared_buffer_)
45 27142747 Dealloc();
46 27145712 }
47
48 1470347915 Item At(const size_t index) const {
49
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1470305936 times.
1470347915 assert(index < size_);
50 1470347915 return buffer_[index];
51 }
52
53 980033396 const Item *AtPtr(const size_t index) const {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 980016698 times.
980033396 assert(index < size_);
55 980033396 return &buffer_[index];
56 }
57
58 1960301391 void PushBack(const Item &item) {
59
2/2
✓ Branch 0 taken 6763 times.
✓ Branch 1 taken 1960280327 times.
1960301391 if (size_ == capacity_)
60 6847 DoubleCapacity();
61
1/2
✓ Branch 2 taken 237 times.
✗ Branch 3 not taken.
1960301391 new (buffer_ + size_) Item(item);
62 1960301391 size_++;
63 1960301391 }
64
65 63051 void Replace(size_t index, const Item &item) {
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 63051 times.
63051 assert(index < size_);
67 63051 buffer_[index] = item;
68 63051 }
69
70 9 bool IsEmpty() const { return size_ == 0; }
71
72 242 void Clear() {
73 242 Dealloc();
74 242 buffer_ = Alloc(kNumInit);
75 242 }
76
77 49 void ShareBuffer(Item **duplicate, bool *large_alloc) {
78 49 *duplicate = buffer_;
79 49 *large_alloc = large_alloc_;
80 49 shared_buffer_ = true;
81 49 }
82
83 6847 void DoubleCapacity() {
84 6847 Item *old_buffer = buffer_;
85 6847 const bool old_large_alloc = large_alloc_;
86
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6763 times.
6847 assert(capacity_ > 0);
88 6847 buffer_ = Alloc(capacity_ * 2);
89
2/2
✓ Branch 0 taken 3288614638 times.
✓ Branch 1 taken 6763 times.
3288635597 for (size_t i = 0; i < size_; ++i)
90
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3288628750 new (buffer_ + i) Item(old_buffer[i]);
91
92 6847 FreeBuffer(old_buffer, size_, old_large_alloc);
93 6847 }
94
95 14149 void ShrinkIfOversized() {
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14149 times.
14149 assert(!shared_buffer_);
97
98
2/2
✓ Branch 0 taken 289 times.
✓ Branch 1 taken 13860 times.
14149 if (size_ <= kNumInit)
99 289 return;
100
2/2
✓ Branch 0 taken 13755 times.
✓ Branch 1 taken 105 times.
13860 if (static_cast<float>(size_) >= (0.25 * static_cast<float>(capacity_)))
101 13755 return;
102
103 105 bool old_large_alloc = large_alloc_;
104 105 Item *new_buffer = Alloc(0.5 * static_cast<float>(capacity_));
105
2/2
✓ Branch 0 taken 11564 times.
✓ Branch 1 taken 105 times.
11669 for (size_t i = 0; i < size_; ++i)
106 11564 new (new_buffer + i) Item(buffer_[i]);
107 105 FreeBuffer(buffer_, size_, old_large_alloc);
108 105 buffer_ = new_buffer;
109 }
110
111 // Careful! Only for externally modified buffer.
112 14149 void SetSize(const size_t new_size) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14149 times.
14149 assert(new_size <= capacity_);
114 14149 size_ = new_size;
115 14149 }
116
117 18175208 size_t size() const { return size_; }
118 784 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 27118459 Item *Alloc(const size_t num_elements) {
125 Item *result;
126 27118459 const size_t num_bytes = sizeof(Item) * num_elements;
127
2/2
✓ Branch 0 taken 2121 times.
✓ Branch 1 taken 13560797 times.
27118459 if (num_bytes >= kMmapThreshold) {
128 2135 result = static_cast<Item *>(smmap(num_bytes));
129 2135 large_alloc_ = true;
130 } else {
131 27116324 result = static_cast<Item *>(smalloc(num_bytes));
132 27115100 large_alloc_ = false;
133 }
134 27117235 capacity_ = num_elements;
135 27117235 return result;
136 }
137
138 27142298 void Dealloc() {
139 27142298 FreeBuffer(buffer_, size_, large_alloc_);
140 27148262 buffer_ = NULL;
141 27148262 capacity_ = 0;
142 27148262 size_ = 0;
143 27148262 }
144
145 27150274 void FreeBuffer(Item *buf, const size_t size, const bool large) {
146
2/2
✓ Branch 0 taken 4758864981 times.
✓ Branch 1 taken 13578383 times.
4786103253 for (size_t i = 0; i < size; ++i)
147 4758952979 buf[i].~Item();
148
149
1/2
✓ Branch 0 taken 13578542 times.
✗ Branch 1 not taken.
27150274 if (buf) {
150
2/2
✓ Branch 0 taken 1974 times.
✓ Branch 1 taken 13576568 times.
27150592 if (large) {
151 1988 smunmap(buf);
152 } else {
153 27148604 free(buf);
154 }
155 }
156 27150274 }
157
158 9003887 void CopyFrom(const BigVector<Item> &other) {
159 9003887 buffer_ = Alloc(other.capacity_);
160
2/2
✓ Branch 0 taken 980014814 times.
✓ Branch 1 taken 9004031 times.
989018845 for (size_t i = 0; i < other.size_; ++i) {
161 980014814 new (buffer_ + i) Item(*other.AtPtr(i));
162 }
163 9004031 size_ = other.size_;
164 9004031 shared_buffer_ = false;
165 9004031 }
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