Skip to content

Commit

Permalink
fix: properly check that circular buffer size is a power of 2
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperPaul123 committed Sep 4, 2024
1 parent c161115 commit 7c5c8fd
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions include/thread_pool/work_stealing_deque.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <atomic>
#include <bit>
#include <cassert>
#include <cstddef>
#include <cstdint>
Expand Down Expand Up @@ -43,14 +44,14 @@ namespace dp {
* @brief Simple circular array buffer that can regrow
*/
class circular_buffer final {
std::int64_t size_;
std::int64_t mask_;
std::size_t size_;
std::size_t mask_;
std::unique_ptr<T[]> buffer_ = std::make_unique_for_overwrite<T[]>(size_);

public:
explicit circular_buffer(const std::int64_t size) : size_(size), mask_(size - 1) {
explicit circular_buffer(const std::size_t size) : size_(size), mask_(size - 1) {
// size must be a power of 2
assert((size % 2) == 0);
assert(std::has_single_bit(size));
}

[[nodiscard]] std::int64_t capacity() const noexcept { return size_; }
Expand Down

0 comments on commit 7c5c8fd

Please sign in to comment.