GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigqueue.h
Date: 2026-02-15 02:35:50
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 1052 BigQueue() {
22 1052 Alloc(kNumInit);
23 1052 size_ = 0;
24 1052 }
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 44 BigQueue(const BigQueue<Item> &other) { CopyFrom(other); }
33
34 180 BigQueue<Item> &operator=(const BigQueue<Item> &other) {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 180 times.
180 if (&other == this)
36 return *this;
37
38 180 Dealloc();
39 180 CopyFrom(other);
40 180 return *this;
41 }
42
43 1048 ~BigQueue() { Dealloc(); }
44
45 880178304 void PushBack(const Item &item) {
46
2/2
✓ Branch 1 taken 2552 times.
✓ Branch 2 taken 880175752 times.
880178304 if (GetAvailableSpace() == 0) {
47 2552 Migrate(1.9 * static_cast<float>(capacity_));
48
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2552 times.
2552 assert(GetAvailableSpace() > 0);
49 }
50
1/2
✓ Branch 2 taken 90304 times.
✗ Branch 3 not taken.
880178304 new (head_ + size_) Item(item);
51 880178304 size_++;
52 880178304 }
53
54 880066144 void PopFront() {
55
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 880066144 times.
880066144 assert(!IsEmpty());
56 880066144 head_++;
57 880066144 size_--;
58
4/4
✓ Branch 0 taken 880057420 times.
✓ Branch 1 taken 8724 times.
✓ Branch 2 taken 2244 times.
✓ Branch 3 taken 880055176 times.
880066144 if ((size_ > kCompactThreshold) && (size_ < (capacity_ / 2)))
59 2244 Migrate(static_cast<int>(static_cast<float>(capacity_ * 0.6)));
60 880066144 }
61
62 880157420 bool Peek(Item **item) {
63
2/2
✓ Branch 1 taken 652 times.
✓ Branch 2 taken 880156768 times.
880157420 if (IsEmpty())
64 652 return false;
65 880156768 *item = head_;
66 880156768 return true;
67 }
68
69 1760223564 bool IsEmpty() const { return size_ == 0; }
70
71 180 void Clear() {
72 180 Dealloc();
73 180 Alloc(kNumInit);
74 180 }
75
76 57796 size_t size() const { return size_; }
77 440 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 1100277220 size_t GetHeadOffset() const { return head_ - buffer_; }
84 880180856 size_t GetAvailableSpace() const {
85 880180856 return capacity_ - (size_ + GetHeadOffset());
86 }
87
88 6252 void Alloc(const size_t num_elements) {
89 6252 size_t num_bytes = sizeof(Item) * num_elements;
90 6252 buffer_ = static_cast<Item *>(smmap(num_bytes));
91 6252 capacity_ = num_elements;
92 6252 head_ = buffer_;
93 6252 }
94
95 1408 void Dealloc() {
96 1408 FreeBuffer(buffer_, GetHeadOffset() + size_);
97 1408 buffer_ = NULL;
98 1408 head_ = NULL;
99 1408 capacity_ = 0;
100 1408 size_ = 0;
101 1408 }
102
103 4796 void Migrate(size_t new_capacity) {
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4796 times.
4796 assert(new_capacity > 0);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4796 times.
4796 assert(new_capacity >= size_);
106
107 4796 size_t head_offset = GetHeadOffset();
108 4796 Item *old_buffer = buffer_;
109
110 4796 Alloc(new_capacity);
111
2/2
✓ Branch 0 taken 2465799732 times.
✓ Branch 1 taken 4796 times.
2465804528 for (size_t i = 0; i < size_; ++i)
112
1/2
✓ Branch 2 taken 147400 times.
✗ Branch 3 not taken.
2465799732 new (buffer_ + i) Item(old_buffer[head_offset + i]);
113
114 4796 FreeBuffer(old_buffer, head_offset + size_);
115 4796 }
116
117 6204 void FreeBuffer(Item *buf, const size_t nitems) {
118
2/2
✓ Branch 0 taken 3566068148 times.
✓ Branch 1 taken 6204 times.
3566074352 for (size_t i = 0; i < nitems; ++i)
119 3566068148 buf[i].~Item();
120
121
1/2
✓ Branch 0 taken 6204 times.
✗ Branch 1 not taken.
6204 if (buf)
122 6204 smunmap(buf);
123 6204 }
124
125 224 void CopyFrom(const BigQueue<Item> &other) {
126 224 size_t min_items = kNumInit;
127 224 Alloc(std::max(other.size_, min_items));
128
2/2
✓ Branch 0 taken 220090160 times.
✓ Branch 1 taken 224 times.
220090384 for (size_t i = 0; i < other.size_; ++i) {
129
1/2
✓ Branch 3 taken 90160 times.
✗ Branch 4 not taken.
220090160 new (buffer_ + i) Item(*(other.buffer_ + other.GetHeadOffset() + i));
130 }
131 224 size_ = other.size_;
132 224 }
133
134 Item *buffer_;
135 Item *head_;
136 size_t size_;
137 size_t capacity_;
138 };
139
140 #endif // CVMFS_BIGQUEUE_H_
141