/src/brpc/src/butil/containers/bounded_queue.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | // Date: Sat Aug 18 12:42:16 CST 2012 |
19 | | |
20 | | // A thread-unsafe bounded queue(ring buffer). It can push/pop from both |
21 | | // sides and is more handy than thread-safe queues in single thread. Use |
22 | | // boost::lockfree::spsc_queue or boost::lockfree::queue in multi-threaded |
23 | | // scenarios. |
24 | | |
25 | | #ifndef BUTIL_BOUNDED_QUEUE_H |
26 | | #define BUTIL_BOUNDED_QUEUE_H |
27 | | |
28 | | #include "butil/macros.h" |
29 | | #include "butil/logging.h" |
30 | | |
31 | | namespace butil { |
32 | | |
33 | | // [Create a on-stack small queue] |
34 | | // char storage[64]; |
35 | | // butil::BoundedQueue<int> q(storage, sizeof(storage), butil::NOT_OWN_STORAGE); |
36 | | // q.push(1); |
37 | | // q.push(2); |
38 | | // ... |
39 | | |
40 | | // [Initialize a class-member queue] |
41 | | // class Foo { |
42 | | // ... |
43 | | // BoundQueue<int> _queue; |
44 | | // }; |
45 | | // int Foo::init() { |
46 | | // BoundedQueue<int> tmp(capacity); |
47 | | // if (!tmp.initialized()) { |
48 | | // LOG(ERROR) << "Fail to create _queue"; |
49 | | // return -1; |
50 | | // } |
51 | | // tmp.swap(_queue); |
52 | | // } |
53 | | |
54 | | enum StorageOwnership { OWNS_STORAGE, NOT_OWN_STORAGE }; |
55 | | |
56 | | template <typename T> |
57 | | class BoundedQueue { |
58 | | public: |
59 | | // You have to pass the memory for storing items at creation. |
60 | | // The queue contains at most memsize/sizeof(T) items. |
61 | | BoundedQueue(void* mem, size_t memsize, StorageOwnership ownership) |
62 | | : _count(0) |
63 | | , _cap(memsize / sizeof(T)) |
64 | | , _start(0) |
65 | | , _ownership(ownership) |
66 | 1.19k | , _items(mem) { |
67 | 1.19k | DCHECK(_items); |
68 | 1.19k | }; Unexecuted instantiation: butil::BoundedQueue<unsigned long>::BoundedQueue(void*, unsigned long, butil::StorageOwnership) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::BoundedQueue(void*, unsigned long, butil::StorageOwnership) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::BoundedQueue(void*, unsigned long, butil::StorageOwnership) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::BoundedQueue(void*, unsigned long, butil::StorageOwnership) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::BoundedQueue(void*, unsigned long, butil::StorageOwnership) Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::BoundedQueue(void*, unsigned long, butil::StorageOwnership) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::BoundedQueue(void*, unsigned long, butil::StorageOwnership) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::BoundedQueue(void*, unsigned long, butil::StorageOwnership) Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::BoundedQueue(void*, unsigned long, butil::StorageOwnership) butil::BoundedQueue<brpc::HPacker::Header>::BoundedQueue(void*, unsigned long, butil::StorageOwnership) Line | Count | Source | 66 | 1.19k | , _items(mem) { | 67 | 1.19k | DCHECK(_items); | 68 | 1.19k | }; |
|
69 | | |
70 | | // Construct a queue with the given capacity. |
71 | | // The malloc() may fail silently, call initialized() to test validity |
72 | | // of the queue. |
73 | | explicit BoundedQueue(size_t capacity) |
74 | | : _count(0) |
75 | | , _cap(capacity) |
76 | | , _start(0) |
77 | | , _ownership(OWNS_STORAGE) |
78 | | , _items(malloc(capacity * sizeof(T))) { |
79 | | DCHECK(_items); |
80 | | }; |
81 | | |
82 | | BoundedQueue() |
83 | | : _count(0) |
84 | | , _cap(0) |
85 | | , _start(0) |
86 | | , _ownership(NOT_OWN_STORAGE) |
87 | 1.19k | , _items(NULL) { |
88 | 1.19k | }; Unexecuted instantiation: butil::BoundedQueue<unsigned long>::BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::BoundedQueue() butil::BoundedQueue<brpc::HPacker::Header>::BoundedQueue() Line | Count | Source | 87 | 1.19k | , _items(NULL) { | 88 | 1.19k | }; |
|
89 | | |
90 | 2.38k | ~BoundedQueue() { |
91 | 2.38k | clear(); |
92 | 2.38k | if (_ownership == OWNS_STORAGE) { |
93 | 1.19k | free(_items); |
94 | 1.19k | _items = NULL; |
95 | 1.19k | } |
96 | 2.38k | } Unexecuted instantiation: butil::BoundedQueue<unsigned long>::~BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::~BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::~BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::~BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::~BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::~BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::~BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::~BoundedQueue() Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::~BoundedQueue() butil::BoundedQueue<brpc::HPacker::Header>::~BoundedQueue() Line | Count | Source | 90 | 2.38k | ~BoundedQueue() { | 91 | 2.38k | clear(); | 92 | 2.38k | if (_ownership == OWNS_STORAGE) { | 93 | 1.19k | free(_items); | 94 | 1.19k | _items = NULL; | 95 | 1.19k | } | 96 | 2.38k | } |
|
97 | | |
98 | | // Push |item| into bottom side of this queue. |
99 | | // Returns true on success, false if queue is full. |
100 | 146 | bool push(const T& item) { |
101 | 146 | if (_count < _cap) { |
102 | 146 | new ((T*)_items + _mod(_start + _count, _cap)) T(item); |
103 | 146 | ++_count; |
104 | 146 | return true; |
105 | 146 | } |
106 | 0 | return false; |
107 | 146 | } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::push(bvar::detail::Sample<bvar::Stat> const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::push(bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::push(bvar::detail::Sample<int> const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::push(bvar::detail::Sample<long> const&) Unexecuted instantiation: butil::BoundedQueue<unsigned long>::push(unsigned long const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::push(bvar::detail::Sample<double> const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::push(bvar::detail::Sample<unsigned long> const&) Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::push(brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item const&) Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::push(brpc::policy::LocalityAwareLoadBalancer::TimeInfo const&) butil::BoundedQueue<brpc::HPacker::Header>::push(brpc::HPacker::Header const&) Line | Count | Source | 100 | 146 | bool push(const T& item) { | 101 | 146 | if (_count < _cap) { | 102 | 146 | new ((T*)_items + _mod(_start + _count, _cap)) T(item); | 103 | 146 | ++_count; | 104 | 146 | return true; | 105 | 146 | } | 106 | 0 | return false; | 107 | 146 | } |
|
108 | | |
109 | | // Push |item| into bottom side of this queue. If the queue is full, |
110 | | // pop topmost item first. |
111 | 0 | void elim_push(const T& item) { |
112 | 0 | if (_count < _cap) { |
113 | 0 | new ((T*)_items + _mod(_start + _count, _cap)) T(item); |
114 | 0 | ++_count; |
115 | 0 | } else { |
116 | 0 | ((T*)_items)[_start] = item; |
117 | 0 | _start = _mod(_start + 1, _cap); |
118 | 0 | } |
119 | 0 | } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::elim_push(bvar::detail::Sample<bvar::Stat> const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::elim_push(bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::elim_push(bvar::detail::Sample<int> const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::elim_push(bvar::detail::Sample<long> const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::elim_push(bvar::detail::Sample<double> const&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::elim_push(bvar::detail::Sample<unsigned long> const&) Unexecuted instantiation: butil::BoundedQueue<unsigned long>::elim_push(unsigned long const&) Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::elim_push(brpc::policy::LocalityAwareLoadBalancer::TimeInfo const&) |
120 | | |
121 | | // Push a default-constructed item into bottom side of this queue |
122 | | // Returns address of the item inside this queue |
123 | | T* push() { |
124 | | if (_count < _cap) { |
125 | | return new ((T*)_items + _mod(_start + _count++, _cap)) T(); |
126 | | } |
127 | | return NULL; |
128 | | } |
129 | | |
130 | | // Push |item| into top side of this queue |
131 | | // Returns true on success, false if queue is full. |
132 | | bool push_top(const T& item) { |
133 | | if (_count < _cap) { |
134 | | _start = _start ? (_start - 1) : (_cap - 1); |
135 | | ++_count; |
136 | | new ((T*)_items + _start) T(item); |
137 | | return true; |
138 | | } |
139 | | return false; |
140 | | } |
141 | | |
142 | | // Push a default-constructed item into top side of this queue |
143 | | // Returns address of the item inside this queue |
144 | | T* push_top() { |
145 | | if (_count < _cap) { |
146 | | _start = _start ? (_start - 1) : (_cap - 1); |
147 | | ++_count; |
148 | | return new ((T*)_items + _start) T(); |
149 | | } |
150 | | return NULL; |
151 | | } |
152 | | |
153 | | // Pop top-most item from this queue |
154 | | // Returns true on success, false if queue is empty |
155 | 0 | bool pop() { |
156 | 0 | if (_count) { |
157 | 0 | --_count; |
158 | 0 | ((T*)_items + _start)->~T(); |
159 | 0 | _start = _mod(_start + 1, _cap); |
160 | 0 | return true; |
161 | 0 | } |
162 | 0 | return false; |
163 | 0 | } Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::pop() Unexecuted instantiation: butil::BoundedQueue<brpc::HPacker::Header>::pop() |
164 | | |
165 | | // Pop top-most item from this queue and copy into |item|. |
166 | | // Returns true on success, false if queue is empty |
167 | 0 | bool pop(T* item) { |
168 | 0 | if (_count) { |
169 | 0 | --_count; |
170 | 0 | T* const p = (T*)_items + _start; |
171 | 0 | *item = *p; |
172 | 0 | p->~T(); |
173 | 0 | _start = _mod(_start + 1, _cap); |
174 | 0 | return true; |
175 | 0 | } |
176 | 0 | return false; |
177 | 0 | } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::pop(bvar::detail::Sample<bvar::Stat>*) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::pop(bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> >*) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::pop(bvar::detail::Sample<int>*) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::pop(bvar::detail::Sample<long>*) Unexecuted instantiation: butil::BoundedQueue<unsigned long>::pop(unsigned long*) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::pop(bvar::detail::Sample<double>*) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::pop(bvar::detail::Sample<unsigned long>*) |
178 | | |
179 | | // Pop bottom-most item from this queue |
180 | | // Returns true on success, false if queue is empty |
181 | | bool pop_bottom() { |
182 | | if (_count) { |
183 | | --_count; |
184 | | ((T*)_items + _mod(_start + _count, _cap))->~T(); |
185 | | return true; |
186 | | } |
187 | | return false; |
188 | | } |
189 | | |
190 | | // Pop bottom-most item from this queue and copy into |item|. |
191 | | // Returns true on success, false if queue is empty |
192 | | bool pop_bottom(T* item) { |
193 | | if (_count) { |
194 | | --_count; |
195 | | T* const p = (T*)_items + _mod(_start + _count, _cap); |
196 | | *item = *p; |
197 | | p->~T(); |
198 | | return true; |
199 | | } |
200 | | return false; |
201 | | } |
202 | | |
203 | | // Pop all items |
204 | 2.38k | void clear() { |
205 | 2.47k | for (uint32_t i = 0; i < _count; ++i) { |
206 | 85 | ((T*)_items + _mod(_start + i, _cap))->~T(); |
207 | 85 | } |
208 | 2.38k | _count = 0; |
209 | 2.38k | _start = 0; |
210 | 2.38k | } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::clear() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::clear() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::clear() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::clear() Unexecuted instantiation: butil::BoundedQueue<unsigned long>::clear() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::clear() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::clear() Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::clear() Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::clear() butil::BoundedQueue<brpc::HPacker::Header>::clear() Line | Count | Source | 204 | 2.38k | void clear() { | 205 | 2.47k | for (uint32_t i = 0; i < _count; ++i) { | 206 | 85 | ((T*)_items + _mod(_start + i, _cap))->~T(); | 207 | 85 | } | 208 | 2.38k | _count = 0; | 209 | 2.38k | _start = 0; | 210 | 2.38k | } |
|
211 | | |
212 | | // Get address of top-most item, NULL if queue is empty |
213 | 0 | T* top() { |
214 | 0 | return _count ? ((T*)_items + _start) : NULL; |
215 | 0 | } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::top() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::top() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::top() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::top() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::top() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::top() Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::top() Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::top() Unexecuted instantiation: butil::BoundedQueue<brpc::HPacker::Header>::top() |
216 | | const T* top() const { |
217 | | return _count ? ((const T*)_items + _start) : NULL; |
218 | | } |
219 | | |
220 | | // Randomly access item from top side. |
221 | | // top(0) == top(), top(size()-1) == bottom() |
222 | | // Returns NULL if |index| is out of range. |
223 | 0 | T* top(size_t index) { |
224 | 0 | if (index < _count) { |
225 | 0 | return (T*)_items + _mod(_start + index, _cap); |
226 | 0 | } |
227 | 0 | return NULL; // including _count == 0 |
228 | 0 | } |
229 | | const T* top(size_t index) const { |
230 | | if (index < _count) { |
231 | | return (const T*)_items + _mod(_start + index, _cap); |
232 | | } |
233 | | return NULL; // including _count == 0 |
234 | | } |
235 | | |
236 | | // Get address of bottom-most item, NULL if queue is empty |
237 | 0 | T* bottom() { |
238 | 0 | return _count ? ((T*)_items + _mod(_start + _count - 1, _cap)) : NULL; |
239 | 0 | } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::bottom() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::bottom() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::bottom() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::bottom() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::bottom() Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::bottom() Unexecuted instantiation: butil::BoundedQueue<unsigned long>::bottom() Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::bottom() |
240 | | const T* bottom() const { |
241 | | return _count ? ((const T*)_items + _mod(_start + _count - 1, _cap)) : NULL; |
242 | | } |
243 | | |
244 | | // Randomly access item from bottom side. |
245 | | // bottom(0) == bottom(), bottom(size()-1) == top() |
246 | | // Returns NULL if |index| is out of range. |
247 | 0 | T* bottom(size_t index) { |
248 | 0 | if (index < _count) { |
249 | 0 | return (T*)_items + _mod(_start + _count - index - 1, _cap); |
250 | 0 | } |
251 | 0 | return NULL; // including _count == 0 |
252 | 0 | } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::bottom(unsigned long) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::bottom(unsigned long) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::bottom(unsigned long) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::bottom(unsigned long) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::bottom(unsigned long) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::bottom(unsigned long) |
253 | 246 | const T* bottom(size_t index) const { |
254 | 246 | if (index < _count) { |
255 | 154 | return (const T*)_items + _mod(_start + _count - index - 1, _cap); |
256 | 154 | } |
257 | 92 | return NULL; // including _count == 0 |
258 | 246 | } Unexecuted instantiation: butil::BoundedQueue<unsigned long>::bottom(unsigned long) const butil::BoundedQueue<brpc::HPacker::Header>::bottom(unsigned long) const Line | Count | Source | 253 | 246 | const T* bottom(size_t index) const { | 254 | 246 | if (index < _count) { | 255 | 154 | return (const T*)_items + _mod(_start + _count - index - 1, _cap); | 256 | 154 | } | 257 | 92 | return NULL; // including _count == 0 | 258 | 246 | } |
|
259 | | |
260 | 0 | bool empty() const { return !_count; } Unexecuted instantiation: butil::BoundedQueue<unsigned long>::empty() const Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::empty() const |
261 | 146 | bool full() const { return _cap == _count; } Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::full() const butil::BoundedQueue<brpc::HPacker::Header>::full() const Line | Count | Source | 261 | 146 | bool full() const { return _cap == _count; } |
|
262 | | |
263 | | // Number of items |
264 | 1.19k | size_t size() const { return _count; } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::size() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::size() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::size() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::size() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::size() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::size() const Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::size() const Unexecuted instantiation: butil::BoundedQueue<unsigned long>::size() const Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::size() const butil::BoundedQueue<brpc::HPacker::Header>::size() const Line | Count | Source | 264 | 1.19k | size_t size() const { return _count; } |
|
265 | | |
266 | | // Maximum number of items that can be in this queue |
267 | 0 | size_t capacity() const { return _cap; } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::capacity() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::capacity() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::capacity() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::capacity() const Unexecuted instantiation: butil::BoundedQueue<unsigned long>::capacity() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::capacity() const Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::capacity() const Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::capacity() const Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::capacity() const |
268 | | |
269 | | // Maximum value of capacity() |
270 | | size_t max_capacity() const { return (1UL << (sizeof(_cap) * 8)) - 1; } |
271 | | |
272 | | // True if the queue was constructed successfully. |
273 | | bool initialized() const { return _items != NULL; } |
274 | | |
275 | | // Swap internal fields with another queue. |
276 | 1.19k | void swap(BoundedQueue& rhs) { |
277 | 1.19k | std::swap(_count, rhs._count); |
278 | 1.19k | std::swap(_cap, rhs._cap); |
279 | 1.19k | std::swap(_start, rhs._start); |
280 | 1.19k | std::swap(_ownership, rhs._ownership); |
281 | 1.19k | std::swap(_items, rhs._items); |
282 | 1.19k | } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::swap(butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::swap(butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::swap(butil::BoundedQueue<bvar::detail::Sample<int> >&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::swap(butil::BoundedQueue<bvar::detail::Sample<long> >&) Unexecuted instantiation: butil::BoundedQueue<unsigned long>::swap(butil::BoundedQueue<unsigned long>&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::swap(butil::BoundedQueue<bvar::detail::Sample<double> >&) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::swap(butil::BoundedQueue<bvar::detail::Sample<unsigned long> >&) butil::BoundedQueue<brpc::HPacker::Header>::swap(butil::BoundedQueue<brpc::HPacker::Header>&) Line | Count | Source | 276 | 1.19k | void swap(BoundedQueue& rhs) { | 277 | 1.19k | std::swap(_count, rhs._count); | 278 | 1.19k | std::swap(_cap, rhs._cap); | 279 | 1.19k | std::swap(_start, rhs._start); | 280 | 1.19k | std::swap(_ownership, rhs._ownership); | 281 | 1.19k | std::swap(_items, rhs._items); | 282 | 1.19k | } |
|
283 | | |
284 | | private: |
285 | | // Since the space is possibly not owned, we disable copying. |
286 | | DISALLOW_COPY_AND_ASSIGN(BoundedQueue); |
287 | | |
288 | | // This is faster than % in this queue because most |off| are smaller |
289 | | // than |cap|. This is probably not true in other place, be careful |
290 | | // before you use this trick. |
291 | 385 | static uint32_t _mod(uint32_t off, uint32_t cap) { |
292 | 385 | while (off >= cap) { |
293 | 0 | off -= cap; |
294 | 0 | } |
295 | 385 | return off; |
296 | 385 | } Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::Stat> >::_mod(unsigned int, unsigned int) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<bvar::detail::PercentileSamples<254ul> > >::_mod(unsigned int, unsigned int) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<int> >::_mod(unsigned int, unsigned int) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<long> >::_mod(unsigned int, unsigned int) Unexecuted instantiation: butil::BoundedQueue<unsigned long>::_mod(unsigned int, unsigned int) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<double> >::_mod(unsigned int, unsigned int) Unexecuted instantiation: butil::BoundedQueue<bvar::detail::Sample<unsigned long> >::_mod(unsigned int, unsigned int) Unexecuted instantiation: butil::BoundedQueue<brpc::SparseMinuteCounter<brpc::ExtendedSocketStat::Sampled>::Item>::_mod(unsigned int, unsigned int) Unexecuted instantiation: butil::BoundedQueue<brpc::policy::LocalityAwareLoadBalancer::TimeInfo>::_mod(unsigned int, unsigned int) butil::BoundedQueue<brpc::HPacker::Header>::_mod(unsigned int, unsigned int) Line | Count | Source | 291 | 385 | static uint32_t _mod(uint32_t off, uint32_t cap) { | 292 | 385 | while (off >= cap) { | 293 | 0 | off -= cap; | 294 | 0 | } | 295 | 385 | return off; | 296 | 385 | } |
|
297 | | |
298 | | uint32_t _count; |
299 | | uint32_t _cap; |
300 | | uint32_t _start; |
301 | | StorageOwnership _ownership; |
302 | | void* _items; |
303 | | }; |
304 | | |
305 | | } // namespace butil |
306 | | |
307 | | #endif // BUTIL_BOUNDED_QUEUE_H |