Skip to content

Commit a323f50

Browse files
committed
chore: use RoundUpPowerOf2() in LockFreeQueue Init()
1 parent a2acc4f commit a323f50

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

trpc/util/queue/lockfree_queue.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <utility>
2424
#include <vector>
2525

26+
#include "trpc/util/algorithm/power_of_two.h"
27+
2628
namespace trpc {
2729

2830
/// @brief Implementation of a thread-safe lock-free queue that supports multiple producers and multiple
@@ -54,21 +56,11 @@ class alignas(64) LockFreeQueue {
5456
return RT_OK;
5557
}
5658

57-
bool size_is_power_of_2 = (size >= 2) && ((size & (size - 1)) == 0);
58-
if (!size_is_power_of_2) {
59-
uint64_t tmp = 1;
60-
while (tmp <= size) {
61-
tmp <<= 1;
62-
}
63-
64-
size = tmp;
65-
}
66-
67-
mask_ = size - 1;
68-
capacity_ = size;
59+
capacity_ = RoundUpPowerOf2(size);
60+
mask_ = capacity_ - 1;
6961

7062
elements_ = std::make_unique<Element[]>(capacity_);
71-
for (size_t i = 0; i < size; ++i) {
63+
for (size_t i = 0; i < capacity_; ++i) {
7264
elements_[i].sequence = i;
7365
}
7466

0 commit comments

Comments
 (0)