diff --git a/utilities/include/bgtu/computer_graphics_lab_work/utilities/default_renderer_linear.hpp b/utilities/include/bgtu/computer_graphics_lab_work/utilities/default_renderer_linear.hpp index 35af234..24b2711 100644 --- a/utilities/include/bgtu/computer_graphics_lab_work/utilities/default_renderer_linear.hpp +++ b/utilities/include/bgtu/computer_graphics_lab_work/utilities/default_renderer_linear.hpp @@ -68,11 +68,15 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { }; Cell *next_unallocated; - Page *last_page; + Page *first_allocated_page; + Page *last_allocated_page; + Page *next_unfinished_page; Page sentinel; public: - ZElementAllocationBuffer() : next_unallocated{nullptr}, sentinel{nullptr, 0}, last_page{&this->sentinel} {}; + ZElementAllocationBuffer() : + next_unallocated{nullptr}, sentinel{nullptr, 0}, + first_allocated_page{&this->sentinel}, last_allocated_page{this->first_allocated_page}, next_unfinished_page{this->first_allocated_page} {}; private: Page *_native_extend(); @@ -165,9 +169,13 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { return &(cell->allocated); } - while ((cell = this->last_page->try_allocate()) == nullptr) - this->_native_extend(); + for (; this->next_unfinished_page != nullptr && (cell = this->next_unfinished_page->try_allocate()) == nullptr; this->next_unfinished_page = this->next_unfinished_page->next) {} + if (cell == nullptr) { + while ((cell = this->last_allocated_page->try_allocate()) == nullptr) + this->_native_extend(); + this->next_unfinished_page = this->last_allocated_page; + } return new(&(cell->allocated)) ZElement{args...}; } diff --git a/utilities/src/default_renderer_linear.cpp b/utilities/src/default_renderer_linear.cpp index d108d38..52e95fa 100644 --- a/utilities/src/default_renderer_linear.cpp +++ b/utilities/src/default_renderer_linear.cpp @@ -19,21 +19,24 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { void DefaultVoxelDrawerCache::ZElementAllocationBuffer::release_resources() { Page *p; - while (this->last_page != nullptr) { - p = this->last_page; - this->last_page = p->next; + while (this->first_allocated_page != nullptr) { + p = this->first_allocated_page; + this->first_allocated_page = p->next; this->_native_free_page(p); } this->next_unallocated = nullptr; + this->last_allocated_page = nullptr; + this->next_unfinished_page = nullptr; } void DefaultVoxelDrawerCache::ZElementAllocationBuffer::reset_allocations() { - Page *p = this->last_page; + Page *p = this->first_allocated_page; while (p != nullptr) { p->reset_allocations(); p = p->next; } this->next_unallocated = nullptr; + this->next_unfinished_page = this->first_allocated_page; } DefaultVoxelDrawerCache::ZElementAllocationBuffer::~ZElementAllocationBuffer() { @@ -50,7 +53,7 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { this->pixel_metadata_cache.size = 0; } - DefaultVoxelDrawerCache::ZElement* DefaultVoxelDrawerCache::sort_zelements_desc(DefaultVoxelDrawerCache::ZElement *start) noexcept { + DefaultVoxelDrawerCache::ZElement *DefaultVoxelDrawerCache::sort_zelements_desc(DefaultVoxelDrawerCache::ZElement *start) noexcept { for (std::size_t frame_size = 1; 0 < frame_size && frame_size < std::numeric_limits::max() / 4; frame_size *= 2) { ZElement **before_left = &start; diff --git a/utilities/src/memory_pages_management.cpp b/utilities/src/memory_pages_management.cpp index 69e6c57..e91a052 100644 --- a/utilities/src/memory_pages_management.cpp +++ b/utilities/src/memory_pages_management.cpp @@ -17,7 +17,8 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities { void *raw = VirtualAlloc(nullptr, max(si.dwPageSize, si.dwAllocationGranularity), MEM_COMMIT, PAGE_READWRITE); if (raw == nullptr) throw std::bad_alloc{}; - return this->last_page = new(raw)Page(this->last_page, Page::calc_capacity(size)); + this->last_allocated_page->next = new(raw)Page(nullptr, Page::calc_capacity(size)); + return this->last_allocated_page = this->last_allocated_page->next; }