Improve CircularBuffer again
This commit is contained in:
parent
45371bc2f0
commit
d8e7772ee5
|
@ -3,68 +3,61 @@
|
|||
#include "array_range.hpp"
|
||||
|
||||
namespace JabyEngine {
|
||||
template<typename T, uint32_t ElementCount>
|
||||
class FastCircularBuffer {
|
||||
private:
|
||||
T* start_adr = nullptr;
|
||||
size_t read_idx = 0;
|
||||
size_t write_idx = 0;
|
||||
template<typename T>
|
||||
class CircularBuffer {
|
||||
private:
|
||||
T* start_adr = nullptr;
|
||||
T* end_adr = nullptr;
|
||||
T* read_adr = nullptr;
|
||||
T* write_adr = nullptr;
|
||||
|
||||
static size_t increment(size_t cur_idx, size_t step) {
|
||||
return ((cur_idx + step) & (ElementCount - 1));
|
||||
T* increment(T* cur_element) const {
|
||||
cur_element += 1;
|
||||
if(cur_element == this->end_adr) {
|
||||
return this->start_adr;
|
||||
}
|
||||
|
||||
public:
|
||||
FastCircularBuffer() = default;
|
||||
return cur_element;
|
||||
}
|
||||
|
||||
size_t setup(T* buffer_start_adr) {
|
||||
this->start_adr = buffer_start_adr;
|
||||
this->read_idx = 0;
|
||||
this->write_idx = 0;
|
||||
public:
|
||||
CircularBuffer() = default;
|
||||
|
||||
return (sizeof(T)*ElementCount);
|
||||
}
|
||||
T* setup(T* buffer_start_adr, size_t elements) {
|
||||
this->start_adr = buffer_start_adr;
|
||||
this->end_adr = &buffer_start_adr[elements];
|
||||
|
||||
T* allocate() {
|
||||
const auto new_idx = FastCircularBuffer::increment(this->write_idx, 1);
|
||||
if(new_idx != this->read_idx) {
|
||||
auto* dst = (this->start_adr + this->write_idx);
|
||||
|
||||
this->write_idx = new_idx;
|
||||
return dst;
|
||||
}
|
||||
this->read_adr = this->start_adr;
|
||||
this->write_adr = this->start_adr;
|
||||
return this->end_adr;
|
||||
}
|
||||
|
||||
T* allocate() {
|
||||
T* cur_adr = this->write_adr;
|
||||
T* next_adr = CircularBuffer::increment(cur_adr);
|
||||
if(next_adr == this->read_adr) {
|
||||
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;
|
||||
else {
|
||||
this->write_adr = next_adr;
|
||||
return cur_adr;
|
||||
}
|
||||
}
|
||||
|
||||
void drop(size_t elements) {
|
||||
this->read_idx = FastCircularBuffer::increment(this->read_idx, elements);
|
||||
T* get_next() const {
|
||||
return this->read_adr;
|
||||
}
|
||||
|
||||
void pop() {
|
||||
if(CircularBuffer::has_data()) {
|
||||
this->read_adr = CircularBuffer::increment(this->read_adr);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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");
|
||||
constexpr bool has_data() const {
|
||||
return (this->read_adr != this->write_adr);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -18,12 +18,12 @@ namespace JabyEngine {
|
|||
};
|
||||
|
||||
private:
|
||||
FileProcessor::State file_pro_state;
|
||||
FastCircularBuffer<CD_IO::DataSector, 8> circular_buffer;
|
||||
LZ4Decompressor lz4_decomp;
|
||||
JobArray jobs;
|
||||
uint8_t* work_area = nullptr;
|
||||
const AutoLBAEntry* lba = nullptr;
|
||||
FileProcessor::State file_pro_state;
|
||||
CircularBuffer<CD_IO::DataSector> circular_buffer;
|
||||
LZ4Decompressor lz4_decomp;
|
||||
JobArray jobs;
|
||||
uint8_t* work_area = nullptr;
|
||||
const AutoLBAEntry* lba = nullptr;
|
||||
|
||||
void start_cur_job();
|
||||
|
||||
|
|
|
@ -21,18 +21,25 @@ namespace JabyEngine {
|
|||
|
||||
void CDFileProcessor :: setup(const volatile AutoLBAEntry* lba, JobArray jobs, uint8_t* work_area) {
|
||||
this->lba = const_cast<const AutoLBAEntry*>(lba);
|
||||
this->work_area = (work_area + this->circular_buffer.setup(reinterpret_cast<CD_IO::DataSector*>(work_area)));
|
||||
this->work_area = reinterpret_cast<uint8_t*>(this->circular_buffer.setup(reinterpret_cast<CD_IO::DataSector*>(work_area), 5));
|
||||
this->jobs = jobs;
|
||||
|
||||
CDFileProcessor::start_cur_job();
|
||||
}
|
||||
|
||||
Progress CDFileProcessor :: process() {
|
||||
const auto test_print = [this](){
|
||||
if(this->circular_buffer.has_data()) {
|
||||
printf("Got-Data: %s\n", this->circular_buffer.get_next());
|
||||
this->circular_buffer.pop();
|
||||
}
|
||||
};
|
||||
|
||||
switch(CD::internal::read_current_state()) {
|
||||
case CD::internal::State::Done:
|
||||
// Need to start next job?
|
||||
// Does the user trigger this?
|
||||
printf("Done: %i\n", this->circular_buffer.has_data());
|
||||
test_print();
|
||||
return Progress::Done;
|
||||
|
||||
case CD::internal::State::BufferFull:
|
||||
|
@ -42,6 +49,7 @@ namespace JabyEngine {
|
|||
|
||||
case CD::internal::State::Reading:
|
||||
// Do we have data? Use it!
|
||||
test_print();
|
||||
return Progress::InProgress;
|
||||
|
||||
case CD::internal::State::Error:
|
||||
|
|
Loading…
Reference in New Issue