GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigqueue.h
Date: 2025-09-14 02:35:40
Exec Total Coverage
Lines: 76 77 98.7%
Branches: 23 32 71.9%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * Similar to bigvector, but queue semantics. Used by the negative entry
5 * tracker. Allocates with mmap in order to avoid memory fragmentation.
6 */
7
8 #ifndef CVMFS_BIGQUEUE_H_
9 #define CVMFS_BIGQUEUE_H_
10
11 #include <algorithm>
12 #include <cassert>
13 #include <cstdlib>
14 #include <new>
15
16 #include "util/smalloc.h"
17
18 template<class Item>
19 class BigQueue {
20 public:
21 935 BigQueue() {
22 935 Alloc(kNumInit);
23 935 size_ = 0;
24 935 }
25
26 explicit BigQueue(const size_t num_items) {
27 const size_t min_items = kNumInit;
28 Alloc(std::max(num_items, min_items));
29 size_ = 0;
30 }
31
32 35 BigQueue(const BigQueue<Item> &other) { CopyFrom(other); }
33
34 116 BigQueue<Item> &operator=(const BigQueue<Item> &other) {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 116 times.
116 if (&other == this)
36 return *this;
37
38 116 Dealloc();
39 116 CopyFrom(other);
40 116 return *this;
41 }
42
43 965 ~BigQueue() { Dealloc(); }
44
45 700145794 void PushBack(const Item &item) {
46
2/2
✓ Branch 1 taken 2050 times.
✓ Branch 2 taken 700143744 times.
700145794 if (GetAvailableSpace() == 0) {
47 2050 Migrate(1.9 * static_cast<float>(capacity_));
48
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2050 times.
2050 assert(GetAvailableSpace() > 0);
49 }
50
1/2
✓ Branch 2 taken 75794 times.
✗ Branch 3 not taken.
700145794 new (head_ + size_) Item(item);
51 700145794 size_++;
52 700145794 }
53
54 700052513 void PopFront() {
55
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 700052513 times.
700052513 assert(!IsEmpty());
56 700052513 head_++;
57 700052513 size_--;
58
4/4
✓ Branch 0 taken 700045675 times.
✓ Branch 1 taken 6838 times.
✓ Branch 2 taken 1785 times.
✓ Branch 3 taken 700043890 times.
700052513 if ((size_ > kCompactThreshold) && (size_ < (capacity_ / 2)))
59 1785 Migrate(static_cast<int>(static_cast<float>(capacity_ * 0.6)));
60 700052513 }
61
62 700128684 bool Peek(Item **item) {
63
2/2
✓ Branch 1 taken 216 times.
✓ Branch 2 taken 700128468 times.
700128684 if (IsEmpty())
64 216 return false;
65 700128468 *item = head_;
66 700128468 return true;
67 }
68
69 1400181197 bool IsEmpty() const { return size_ == 0; }
70
71 116 void Clear() {
72 116 Dealloc();
73 116 Alloc(kNumInit);
74 116 }
75
76 48343 size_t size() const { return size_; }
77 350 size_t capacity() const { return capacity_; }
78
79 private:
80 static const size_t kNumInit = 64;
81 static const size_t kCompactThreshold = 64;
82
83 875228657 size_t GetHeadOffset() const { return head_ - buffer_; }
84 700147844 size_t GetAvailableSpace() const {
85 700147844 return capacity_ - (size_ + GetHeadOffset());
86 }
87
88 5037 void Alloc(const size_t num_elements) {
89 5037 size_t num_bytes = sizeof(Item) * num_elements;
90 5037 buffer_ = static_cast<Item *>(smmap(num_bytes));
91 5037 capacity_ = num_elements;
92 5037 head_ = buffer_;
93 5037 }
94
95 1197 void Dealloc() {
96 1197 FreeBuffer(buffer_, GetHeadOffset() + size_);
97 1197 buffer_ = NULL;
98 1197 head_ = NULL;
99 1197 capacity_ = 0;
100 1197 size_ = 0;
101 1197 }
102
103 3835 void Migrate(size_t new_capacity) {
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3835 times.
3835 assert(new_capacity > 0);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3835 times.
3835 assert(new_capacity >= size_);
106
107 3835 size_t head_offset = GetHeadOffset();
108 3835 Item *old_buffer = buffer_;
109
110 3835 Alloc(new_capacity);
111
2/2
✓ Branch 0 taken 1961438305 times.
✓ Branch 1 taken 3835 times.
1961442140 for (size_t i = 0; i < size_; ++i)
112
1/2
✓ Branch 2 taken 123950 times.
✗ Branch 3 not taken.
1961438305 new (buffer_ + i) Item(old_buffer[head_offset + i]);
113
114 3835 FreeBuffer(old_buffer, head_offset + size_);
115 3835 }
116
117 5032 void FreeBuffer(Item *buf, const size_t nitems) {
118
2/2
✓ Branch 0 taken 2836659875 times.
✓ Branch 1 taken 5032 times.
2836664907 for (size_t i = 0; i < nitems; ++i)
119 2836659875 buf[i].~Item();
120
121
1/2
✓ Branch 0 taken 5032 times.
✗ Branch 1 not taken.
5032 if (buf)
122 5032 smunmap(buf);
123 5032 }
124
125 151 void CopyFrom(const BigQueue<Item> &other) {
126 151 size_t min_items = kNumInit;
127 151 Alloc(std::max(other.size_, min_items));
128
2/2
✓ Branch 0 taken 175075781 times.
✓ Branch 1 taken 151 times.
175075932 for (size_t i = 0; i < other.size_; ++i) {
129
1/2
✓ Branch 3 taken 75781 times.
✗ Branch 4 not taken.
175075781 new (buffer_ + i) Item(*(other.buffer_ + other.GetHeadOffset() + i));
130 }
131 151 size_ = other.size_;
132 151 }
133
134 Item *buffer_;
135 Item *head_;
136 size_t size_;
137 size_t capacity_;
138 };
139
140 #endif // CVMFS_BIGQUEUE_H_
141