Fixing memory leakage in default renderer's allocator
This commit is contained in:
parent
056a3efc73
commit
e4442e595a
@ -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...};
|
||||
}
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user