GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/bigqueue.h
Date: 2026-05-24 02:35:55
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 703 BigQueue() {
22 703 Alloc(kNumInit);
23 703 size_ = 0;
24 703 }
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 31 BigQueue(const BigQueue<Item> &other) { CopyFrom(other); }
33
34 56 BigQueue<Item> &operator=(const BigQueue<Item> &other) {
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (&other == this)
36 return *this;
37
38 56 Dealloc();
39 56 CopyFrom(other);
40 56 return *this;
41 }
42
43 717 ~BigQueue() { Dealloc(); }
44
45 620088692 void PushBack(const Item &item) {
46
2/2
✓ Branch 1 taken 1618 times.
✓ Branch 2 taken 620087074 times.
620088692 if (GetAvailableSpace() == 0) {
47 1618 Migrate(1.9 * static_cast<float>(capacity_));
48
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1618 times.
1618 assert(GetAvailableSpace() > 0);
49 }
50
1/2
✓ Branch 2 taken 26692 times.
✗ Branch 3 not taken.
620088692 new (head_ + size_) Item(item);
51 620088692 size_++;
52 620088692 }
53
54 620046551 void PopFront() {
55
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 620046551 times.
620046551 assert(!IsEmpty());
56 620046551 head_++;
57 620046551 size_--;
58
4/4
✓ Branch 0 taken 620040455 times.
✓ Branch 1 taken 6096 times.
✓ Branch 2 taken 1581 times.
✓ Branch 3 taken 620038874 times.
620046551 if ((size_ > kCompactThreshold) && (size_ < (capacity_ / 2)))
59 1581 Migrate(static_cast<int>(static_cast<float>(capacity_ * 0.6)));
60 620046551 }
61
62 620073618 bool Peek(Item **item) {
63
2/2
✓ Branch 1 taken 272 times.
✓ Branch 2 taken 620073346 times.
620073618 if (IsEmpty())
64 272 return false;
65 620073346 *item = head_;
66 620073346 return true;
67 }
68
69 1240120169 bool IsEmpty() const { return size_ == 0; }
70
71 56 void Clear() {
72 56 Dealloc();
73 56 Alloc(kNumInit);
74 56 }
75
76 17507 size_t size() const { return size_; }
77 310 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 775120979 size_t GetHeadOffset() const { return head_ - buffer_; }
84 620090310 size_t GetAvailableSpace() const {
85 620090310 return capacity_ - (size_ + GetHeadOffset());
86 }
87
88 4045 void Alloc(const size_t num_elements) {
89 4045 size_t num_bytes = sizeof(Item) * num_elements;
90 4045 buffer_ = static_cast<Item *>(smmap(num_bytes));
91 4045 capacity_ = num_elements;
92 4045 head_ = buffer_;
93 4045 }
94
95 829 void Dealloc() {
96 829 FreeBuffer(buffer_, GetHeadOffset() + size_);
97 829 buffer_ = NULL;
98 829 head_ = NULL;
99 829 capacity_ = 0;
100 829 size_ = 0;
101 829 }
102
103 3199 void Migrate(size_t new_capacity) {
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3199 times.
3199 assert(new_capacity > 0);
105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3199 times.
3199 assert(new_capacity >= size_);
106
107 3199 size_t head_offset = GetHeadOffset();
108 3199 Item *old_buffer = buffer_;
109
110 3199 Alloc(new_capacity);
111
2/2
✓ Branch 0 taken 1737207693 times.
✓ Branch 1 taken 3199 times.
1737210892 for (size_t i = 0; i < size_; ++i)
112
1/2
✓ Branch 2 taken 43550 times.
✗ Branch 3 not taken.
1737207693 new (buffer_ + i) Item(old_buffer[head_offset + i]);
113
114 3199 FreeBuffer(old_buffer, head_offset + size_);
115 3199 }
116
117 4028 void FreeBuffer(Item *buf, const size_t nitems) {
118
2/2
✓ Branch 0 taken 2512323009 times.
✓ Branch 1 taken 4028 times.
2512327037 for (size_t i = 0; i < nitems; ++i)
119 2512323009 buf[i].~Item();
120
121
1/2
✓ Branch 0 taken 4028 times.
✗ Branch 1 not taken.
4028 if (buf)
122 4028 smunmap(buf);
123 4028 }
124
125 87 void CopyFrom(const BigQueue<Item> &other) {
126 87 size_t min_items = kNumInit;
127 87 Alloc(std::max(other.size_, min_items));
128
2/2
✓ Branch 0 taken 155026641 times.
✓ Branch 1 taken 87 times.
155026728 for (size_t i = 0; i < other.size_; ++i) {
129
1/2
✓ Branch 3 taken 26641 times.
✗ Branch 4 not taken.
155026641 new (buffer_ + i) Item(*(other.buffer_ + other.GetHeadOffset() + i));
130 }
131 87 size_ = other.size_;
132 87 }
133
134 Item *buffer_;
135 Item *head_;
136 size_t size_;
137 size_t capacity_;
138 };
139
140 #endif // CVMFS_BIGQUEUE_H_
141