Fixing memory leakage in default renderer's allocator

This commit is contained in:
Andrew Golovashevich 2024-12-10 15:08:21 +03:00
parent 056a3efc73
commit e4442e595a
3 changed files with 22 additions and 10 deletions

View File

@ -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...};
}

View File

@ -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<std::size_t>::max() / 4; frame_size *= 2) {
ZElement **before_left = &start;

View File

@ -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;
}