Kernel: Allocators are now stored in UniqPtr

This allows proper memory management, we had some memory leak
This commit is contained in:
Bananymous
2023-06-04 01:25:57 +03:00
parent 1b1f22c35e
commit 5bf7ca1c80
6 changed files with 47 additions and 41 deletions

View File

@@ -3,6 +3,14 @@
namespace Kernel
{
BAN::ErrorOr<BAN::UniqPtr<FixedWidthAllocator>> FixedWidthAllocator::create(PageTable& page_table, uint32_t allocation_size)
{
auto* allocator = new FixedWidthAllocator(page_table, allocation_size);
if (allocator == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::UniqPtr<FixedWidthAllocator>::adopt(allocator);
}
FixedWidthAllocator::FixedWidthAllocator(PageTable& page_table, uint32_t allocation_size)
: m_page_table(page_table)
, m_allocation_size(BAN::Math::max(allocation_size, m_min_allocation_size))
@@ -222,14 +230,11 @@ namespace Kernel
ASSERT_NOT_REACHED();
}
BAN::ErrorOr<FixedWidthAllocator*> FixedWidthAllocator::clone(PageTable& new_page_table)
BAN::ErrorOr<BAN::UniqPtr<FixedWidthAllocator>> FixedWidthAllocator::clone(PageTable& new_page_table)
{
FixedWidthAllocator* allocator = new FixedWidthAllocator(new_page_table, allocation_size());
if (allocator == nullptr)
return BAN::Error::from_errno(ENOMEM);
auto allocator = TRY(FixedWidthAllocator::create(new_page_table, allocation_size()));
m_page_table.lock();
ASSERT(m_page_table.is_page_free(0));
for (node* node = m_used_list; node; node = node->next)

View File

@@ -1,8 +1,17 @@
#include <kernel/Memory/GeneralAllocator.h>
#include <kernel/Memory/PageTableScope.h>
namespace Kernel
{
BAN::ErrorOr<BAN::UniqPtr<GeneralAllocator>> GeneralAllocator::create(PageTable& page_table)
{
auto* allocator = new GeneralAllocator(page_table);
if (allocator == nullptr)
return BAN::Error::from_errno(ENOMEM);
return BAN::UniqPtr<GeneralAllocator>::adopt(allocator);
}
GeneralAllocator::GeneralAllocator(PageTable& page_table)
: m_page_table(page_table)
{ }
@@ -60,11 +69,9 @@ namespace Kernel
return false;
}
BAN::ErrorOr<GeneralAllocator*> GeneralAllocator::clone(PageTable& new_page_table)
BAN::ErrorOr<BAN::UniqPtr<GeneralAllocator>> GeneralAllocator::clone(PageTable& new_page_table)
{
GeneralAllocator* allocator = new GeneralAllocator(new_page_table);
if (allocator == nullptr)
return BAN::Error::from_errno(ENOMEM);
auto allocator = TRY(GeneralAllocator::create(new_page_table));
m_page_table.lock();