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; Cell *next_unallocated;
Page *last_page; Page *first_allocated_page;
Page *last_allocated_page;
Page *next_unfinished_page;
Page sentinel; Page sentinel;
public: 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: private:
Page *_native_extend(); Page *_native_extend();
@ -165,9 +169,13 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities {
return &(cell->allocated); return &(cell->allocated);
} }
while ((cell = this->last_page->try_allocate()) == nullptr) for (; this->next_unfinished_page != nullptr && (cell = this->next_unfinished_page->try_allocate()) == nullptr; this->next_unfinished_page = this->next_unfinished_page->next) {}
this->_native_extend();
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...}; return new(&(cell->allocated)) ZElement{args...};
} }

View File

@ -19,21 +19,24 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities {
void DefaultVoxelDrawerCache::ZElementAllocationBuffer::release_resources() { void DefaultVoxelDrawerCache::ZElementAllocationBuffer::release_resources() {
Page *p; Page *p;
while (this->last_page != nullptr) { while (this->first_allocated_page != nullptr) {
p = this->last_page; p = this->first_allocated_page;
this->last_page = p->next; this->first_allocated_page = p->next;
this->_native_free_page(p); this->_native_free_page(p);
} }
this->next_unallocated = nullptr; this->next_unallocated = nullptr;
this->last_allocated_page = nullptr;
this->next_unfinished_page = nullptr;
} }
void DefaultVoxelDrawerCache::ZElementAllocationBuffer::reset_allocations() { void DefaultVoxelDrawerCache::ZElementAllocationBuffer::reset_allocations() {
Page *p = this->last_page; Page *p = this->first_allocated_page;
while (p != nullptr) { while (p != nullptr) {
p->reset_allocations(); p->reset_allocations();
p = p->next; p = p->next;
} }
this->next_unallocated = nullptr; this->next_unallocated = nullptr;
this->next_unfinished_page = this->first_allocated_page;
} }
DefaultVoxelDrawerCache::ZElementAllocationBuffer::~ZElementAllocationBuffer() { DefaultVoxelDrawerCache::ZElementAllocationBuffer::~ZElementAllocationBuffer() {

View File

@ -17,7 +17,8 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities {
void *raw = VirtualAlloc(nullptr, max(si.dwPageSize, si.dwAllocationGranularity), MEM_COMMIT, PAGE_READWRITE); void *raw = VirtualAlloc(nullptr, max(si.dwPageSize, si.dwAllocationGranularity), MEM_COMMIT, PAGE_READWRITE);
if (raw == nullptr) throw std::bad_alloc{}; 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;
} }