| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/bigqueue.h |
| Date: | 2026-04-26 02:35:59 |
| 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 | 981 | BigQueue() { | |
| 22 | 981 | Alloc(kNumInit); | |
| 23 | 981 | size_ = 0; | |
| 24 | 981 | } | |
| 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 | 38 | BigQueue(const BigQueue<Item> &other) { CopyFrom(other); } | |
| 33 | |||
| 34 | 144 | BigQueue<Item> &operator=(const BigQueue<Item> &other) { | |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 144 times.
|
144 | if (&other == this) |
| 36 | ✗ | return *this; | |
| 37 | |||
| 38 | 144 | Dealloc(); | |
| 39 | 144 | CopyFrom(other); | |
| 40 | 144 | return *this; | |
| 41 | } | ||
| 42 | |||
| 43 | 977 | ~BigQueue() { Dealloc(); } | |
| 44 | |||
| 45 | 760145806 | void PushBack(const Item &item) { | |
| 46 |
2/2✓ Branch 1 taken 2164 times.
✓ Branch 2 taken 760143642 times.
|
760145806 | if (GetAvailableSpace() == 0) { |
| 47 | 2164 | Migrate(1.9 * static_cast<float>(capacity_)); | |
| 48 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2164 times.
|
2164 | assert(GetAvailableSpace() > 0); |
| 49 | } | ||
| 50 |
1/2✓ Branch 2 taken 69806 times.
✗ Branch 3 not taken.
|
760145806 | new (head_ + size_) Item(item); |
| 51 | 760145806 | size_++; | |
| 52 | 760145806 | } | |
| 53 | |||
| 54 | 760057132 | void PopFront() { | |
| 55 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 760057132 times.
|
760057132 | assert(!IsEmpty()); |
| 56 | 760057132 | head_++; | |
| 57 | 760057132 | size_--; | |
| 58 |
4/4✓ Branch 0 taken 760049590 times.
✓ Branch 1 taken 7542 times.
✓ Branch 2 taken 1938 times.
✓ Branch 3 taken 760047652 times.
|
760057132 | if ((size_ > kCompactThreshold) && (size_ < (capacity_ / 2))) |
| 59 | 1938 | Migrate(static_cast<int>(static_cast<float>(capacity_ * 0.6))); | |
| 60 | 760057132 | } | |
| 61 | |||
| 62 | 760127780 | bool Peek(Item **item) { | |
| 63 |
2/2✓ Branch 1 taken 576 times.
✓ Branch 2 taken 760127204 times.
|
760127780 | if (IsEmpty()) |
| 64 | 576 | return false; | |
| 65 | 760127204 | *item = head_; | |
| 66 | 760127204 | return true; | |
| 67 | } | ||
| 68 | |||
| 69 | 1520184912 | bool IsEmpty() const { return size_ == 0; } | |
| 70 | |||
| 71 | 144 | void Clear() { | |
| 72 | 144 | Dealloc(); | |
| 73 | 144 | Alloc(kNumInit); | |
| 74 | 144 | } | |
| 75 | |||
| 76 | 44788 | size_t size() const { return size_; } | |
| 77 | 380 | 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 | 950223011 | size_t GetHeadOffset() const { return head_ - buffer_; } | |
| 84 | 760147970 | size_t GetAvailableSpace() const { | |
| 85 | 760147970 | return capacity_ - (size_ + GetHeadOffset()); | |
| 86 | } | ||
| 87 | |||
| 88 | 5409 | void Alloc(const size_t num_elements) { | |
| 89 | 5409 | size_t num_bytes = sizeof(Item) * num_elements; | |
| 90 | 5409 | buffer_ = static_cast<Item *>(smmap(num_bytes)); | |
| 91 | 5409 | capacity_ = num_elements; | |
| 92 | 5409 | head_ = buffer_; | |
| 93 | 5409 | } | |
| 94 | |||
| 95 | 1265 | void Dealloc() { | |
| 96 | 1265 | FreeBuffer(buffer_, GetHeadOffset() + size_); | |
| 97 | 1265 | buffer_ = NULL; | |
| 98 | 1265 | head_ = NULL; | |
| 99 | 1265 | capacity_ = 0; | |
| 100 | 1265 | size_ = 0; | |
| 101 | 1265 | } | |
| 102 | |||
| 103 | 4102 | void Migrate(size_t new_capacity) { | |
| 104 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4102 times.
|
4102 | assert(new_capacity > 0); |
| 105 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4102 times.
|
4102 | assert(new_capacity >= size_); |
| 106 | |||
| 107 | 4102 | size_t head_offset = GetHeadOffset(); | |
| 108 | 4102 | Item *old_buffer = buffer_; | |
| 109 | |||
| 110 | 4102 | Alloc(new_capacity); | |
| 111 |
2/2✓ Branch 0 taken 2129540914 times.
✓ Branch 1 taken 4102 times.
|
2129545016 | for (size_t i = 0; i < size_; ++i) |
| 112 |
1/2✓ Branch 2 taken 113900 times.
✗ Branch 3 not taken.
|
2129540914 | new (buffer_ + i) Item(old_buffer[head_offset + i]); |
| 113 | |||
| 114 | 4102 | FreeBuffer(old_buffer, head_offset + size_); | |
| 115 | 4102 | } | |
| 116 | |||
| 117 | 5367 | void FreeBuffer(Item *buf, const size_t nitems) { | |
| 118 |
2/2✓ Branch 0 taken 3079756352 times.
✓ Branch 1 taken 5367 times.
|
3079761719 | for (size_t i = 0; i < nitems; ++i) |
| 119 | 3079756352 | buf[i].~Item(); | |
| 120 | |||
| 121 |
1/2✓ Branch 0 taken 5367 times.
✗ Branch 1 not taken.
|
5367 | if (buf) |
| 122 | 5367 | smunmap(buf); | |
| 123 | 5367 | } | |
| 124 | |||
| 125 | 182 | void CopyFrom(const BigQueue<Item> &other) { | |
| 126 | 182 | size_t min_items = kNumInit; | |
| 127 | 182 | Alloc(std::max(other.size_, min_items)); | |
| 128 |
2/2✓ Branch 0 taken 190069674 times.
✓ Branch 1 taken 182 times.
|
190069856 | for (size_t i = 0; i < other.size_; ++i) { |
| 129 |
1/2✓ Branch 3 taken 69674 times.
✗ Branch 4 not taken.
|
190069674 | new (buffer_ + i) Item(*(other.buffer_ + other.GetHeadOffset() + i)); |
| 130 | } | ||
| 131 | 182 | size_ = other.size_; | |
| 132 | 182 | } | |
| 133 | |||
| 134 | Item *buffer_; | ||
| 135 | Item *head_; | ||
| 136 | size_t size_; | ||
| 137 | size_t capacity_; | ||
| 138 | }; | ||
| 139 | |||
| 140 | #endif // CVMFS_BIGQUEUE_H_ | ||
| 141 |