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