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;
|
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...};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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() {
|
||||||
@ -50,7 +53,7 @@ namespace BGTU::ComputerGraphicsLabWork::Utilities {
|
|||||||
this->pixel_metadata_cache.size = 0;
|
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) {
|
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;
|
ZElement **before_left = &start;
|
||||||
|
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user