Skip to content

Commit

Permalink
fix circular buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Nov 13, 2023
1 parent 478eea9 commit 23989d9
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions common/circular_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ class CircularBuffer {
T& next() { return data_[first_.get() % SIZE]; }

size_t size() const { return first_.get() - last_.get(); }
bool empty() const { size() == 0; }
size_t space_available() const { return SIZE - size(); }
size_t continuous_space() const {
return std::min<size_t>(space_available(), SIZE - last_.get() % SIZE);
}
size_t continuous_data() const {
return std::min<size_t>(size(), last_.get() % SIZE);
return std::min<size_t>(size(), SIZE - first_.get() % SIZE);
}

void push() { first_ += 1; }
void push(size_t elements) { first_ += elements; }
void pop() { last_ += 1; }
T pop() {
T ret = current();
last_ += 1;
return ret;
}
void pop(size_t elements) { last_ += elements; }

void clear() { first_ = 0; last_=0; }
size_t pos() { return last_.get(); }

private:
T data_[SIZE];
POAtomic<size_t> first_;
Expand Down

0 comments on commit 23989d9

Please sign in to comment.