Introduce FastCircularBuffer
This commit is contained in:
parent
bce13e12ff
commit
82c25693a9
|
@ -0,0 +1,17 @@
|
||||||
|
#ifndef __JABYENGINE_ARRAY_RANGE_HPP__
|
||||||
|
#define __JABYENGINE_ARRAY_RANGE_HPP__
|
||||||
|
#include "../../stddef.h"
|
||||||
|
|
||||||
|
namespace JabyEngine {
|
||||||
|
template<typename T>
|
||||||
|
struct ArrayRange {
|
||||||
|
T* start = nullptr;
|
||||||
|
size_t size = 0;
|
||||||
|
|
||||||
|
constexpr ArrayRange() = default;
|
||||||
|
constexpr ArrayRange(T* start, size_t size) : start(start), size(size) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //!__JABYENGINE_ARRAY_RANGE_HPP__
|
|
@ -1,40 +1,30 @@
|
||||||
#ifndef __JABYENGINE_CIRCULAR_BUFFER_HPP__
|
#ifndef __JABYENGINE_CIRCULAR_BUFFER_HPP__
|
||||||
#define __JABYENGINE_CIRCULAR_BUFFER_HPP__
|
#define __JABYENGINE_CIRCULAR_BUFFER_HPP__
|
||||||
#include "../../stddef.h"
|
#include "array_range.hpp"
|
||||||
|
|
||||||
namespace JabyEngine {
|
namespace JabyEngine {
|
||||||
class CircularBuffer {
|
template<typename T, uint32_t ElementCount>
|
||||||
|
class FastCircularBuffer {
|
||||||
private:
|
private:
|
||||||
typedef uint32_t T;
|
|
||||||
|
|
||||||
T* start_adr = nullptr;
|
T* start_adr = nullptr;
|
||||||
size_t end_idx = 0;
|
|
||||||
size_t read_idx = 0;
|
size_t read_idx = 0;
|
||||||
size_t write_idx = 0;
|
size_t write_idx = 0;
|
||||||
|
|
||||||
static size_t increment(size_t cur_idx, size_t end_idx) {
|
static size_t increment(size_t cur_idx, size_t step) {
|
||||||
static constexpr size_t Step = 1;
|
return ((cur_idx + step) & (ElementCount - 1));
|
||||||
|
|
||||||
cur_idx += Step;
|
|
||||||
if(cur_idx >= end_idx) {
|
|
||||||
return (end_idx - cur_idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
return cur_idx;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CircularBuffer() = default;
|
FastCircularBuffer() = default;
|
||||||
|
|
||||||
void setup(T* buffer_start_adr, size_t element_count) {
|
void setup(T* buffer_start_adr, size_t element_count) {
|
||||||
this->start_adr = buffer_start_adr;
|
this->start_adr = buffer_start_adr;
|
||||||
this->end_idx = element_count;
|
|
||||||
this->read_idx = 0;
|
this->read_idx = 0;
|
||||||
this->write_idx = 0;
|
this->write_idx = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
T* push() {
|
T* allocate() {
|
||||||
const auto new_idx = CircularBuffer::increment(this->write_idx, this->end_idx);
|
const auto new_idx = FastCircularBuffer::increment(this->write_idx, 1);
|
||||||
if(new_idx != this->read_idx) {
|
if(new_idx != this->read_idx) {
|
||||||
auto* dst = (this->start_adr + this->write_idx);
|
auto* dst = (this->start_adr + this->write_idx);
|
||||||
|
|
||||||
|
@ -45,9 +35,34 @@ namespace JabyEngine {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const T* pop() {
|
||||||
|
if(this->write_idx != this->read_idx) {
|
||||||
|
const auto* src = (this->start_adr + this->read_idx);
|
||||||
|
FastCircularBuffer::drop(1);
|
||||||
|
|
||||||
|
return src;
|
||||||
|
}
|
||||||
|
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void drop(size_t elements) {
|
||||||
|
this->read_idx = FastCircularBuffer::increment(this->read_idx, elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr ArrayRange<T> get_first_continious() const {
|
||||||
|
return {&this->start_adr[this->read_idx], (this->write_idx >= this->read_idx) ? (this->write_idx - this->read_idx) : (ElementCount - this->read_idx)};
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr ArrayRange<T> get_second_continious() const {
|
||||||
|
return {this->start_adr, (this->write_idx < this->read_idx) ? this->write_idx : 0};
|
||||||
|
}
|
||||||
|
|
||||||
constexpr bool has_data() const {
|
constexpr bool has_data() const {
|
||||||
return (this->read_idx != this->write_idx);
|
return (this->read_idx != this->write_idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static_assert(ElementCount == 2 || ElementCount == 4 || ElementCount == 8 || ElementCount == 16 || ElementCount == 32 || ElementCount == 64 || ElementCount == 128 || ElementCount == 256, "ElementCount for FastCircularBuffer must be power of 2");
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue