From b7f3d40a7c1e7c692483b9df82dd7d0caa030970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gaier?= Date: Fri, 3 Mar 2023 17:30:45 +0100 Subject: [PATCH] Improve CircularBuffer again --- include/PSX/Auxiliary/circular_buffer.hpp | 91 +++++++++---------- .../PSX/File/Processor/cd_file_processor.hpp | 12 +-- .../src/File/Processor/cd_file_processor.cpp | 12 ++- 3 files changed, 58 insertions(+), 57 deletions(-) diff --git a/include/PSX/Auxiliary/circular_buffer.hpp b/include/PSX/Auxiliary/circular_buffer.hpp index 82ace72d..c68f3631 100644 --- a/include/PSX/Auxiliary/circular_buffer.hpp +++ b/include/PSX/Auxiliary/circular_buffer.hpp @@ -3,68 +3,61 @@ #include "array_range.hpp" namespace JabyEngine { - template - class FastCircularBuffer { - private: - T* start_adr = nullptr; - size_t read_idx = 0; - size_t write_idx = 0; + template + 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; - - size_t setup(T* buffer_start_adr) { - this->start_adr = buffer_start_adr; - this->read_idx = 0; - this->write_idx = 0; + return cur_element; + } - 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() { - 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 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 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); + } }; } diff --git a/include/PSX/File/Processor/cd_file_processor.hpp b/include/PSX/File/Processor/cd_file_processor.hpp index 09f6abc6..bd05a252 100644 --- a/include/PSX/File/Processor/cd_file_processor.hpp +++ b/include/PSX/File/Processor/cd_file_processor.hpp @@ -18,12 +18,12 @@ namespace JabyEngine { }; private: - FileProcessor::State file_pro_state; - FastCircularBuffer circular_buffer; - LZ4Decompressor lz4_decomp; - JobArray jobs; - uint8_t* work_area = nullptr; - const AutoLBAEntry* lba = nullptr; + FileProcessor::State file_pro_state; + CircularBuffer circular_buffer; + LZ4Decompressor lz4_decomp; + JobArray jobs; + uint8_t* work_area = nullptr; + const AutoLBAEntry* lba = nullptr; void start_cur_job(); diff --git a/src/Library/src/File/Processor/cd_file_processor.cpp b/src/Library/src/File/Processor/cd_file_processor.cpp index 635a1be8..41c79ecd 100644 --- a/src/Library/src/File/Processor/cd_file_processor.cpp +++ b/src/Library/src/File/Processor/cd_file_processor.cpp @@ -21,18 +21,25 @@ namespace JabyEngine { void CDFileProcessor :: setup(const volatile AutoLBAEntry* lba, JobArray jobs, uint8_t* work_area) { this->lba = const_cast(lba); - this->work_area = (work_area + this->circular_buffer.setup(reinterpret_cast(work_area))); + this->work_area = reinterpret_cast(this->circular_buffer.setup(reinterpret_cast(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: