Compare commits

..

No commits in common. "ed34009b50d733380d6d7deb5c4bb4e89e243802" and "a1eca479d551482a630a64efae19f12198fb1a3a" have entirely different histories.

2 changed files with 5 additions and 26 deletions

View File

@ -125,14 +125,8 @@ static Option<HeapBlock*> split(HeapBlock* block, usize size)
static Result<void> combine_forward(HeapBlock* block) static Result<void> combine_forward(HeapBlock* block)
{ {
// This block ends a memory range, cannot be combined with blocks outside its range.
if (block->status & BLOCK_END_MEM) return {};
// The caller needs to ensure there is a next block. // The caller needs to ensure there is a next block.
HeapBlock* const next = heap.next(block).value(); HeapBlock* const next = heap.next(block).value();
// This block starts a memory range, cannot be combined with blocks outside its range.
if (next->status & BLOCK_START_MEM) return {};
heap.remove(next); heap.remove(next);
next->magic = BLOCK_DEAD; next->magic = BLOCK_DEAD;
@ -140,8 +134,7 @@ static Result<void> combine_forward(HeapBlock* block)
{ {
if (next->status & BLOCK_START_MEM) if (next->status & BLOCK_START_MEM)
{ {
const usize pages = get_blocks_from_size(next->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE); TRY(release_pages(next, get_blocks_from_size(next->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE)));
TRY(release_pages(next, pages));
return {}; return {};
} }
else else
@ -155,13 +148,8 @@ static Result<void> combine_forward(HeapBlock* block)
static Result<HeapBlock*> combine_backward(HeapBlock* block) static Result<HeapBlock*> combine_backward(HeapBlock* block)
{ {
// This block starts a memory range, cannot be combined with blocks outside its range.
if (block->status & BLOCK_START_MEM) return block;
// The caller needs to ensure there is a last block. // The caller needs to ensure there is a last block.
HeapBlock* const last = heap.previous(block).value(); HeapBlock* const last = heap.previous(block).value();
// This block ends a memory range, cannot be combined with blocks outside its range.
if (last->status & BLOCK_END_MEM) return block;
heap.remove(block); heap.remove(block);
block->magic = BLOCK_DEAD; block->magic = BLOCK_DEAD;
@ -169,8 +157,7 @@ static Result<HeapBlock*> combine_backward(HeapBlock* block)
{ {
if (block->status & BLOCK_START_MEM) if (block->status & BLOCK_START_MEM)
{ {
const usize pages = get_blocks_from_size(block->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE); TRY(release_pages(block, get_blocks_from_size(block->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE)));
TRY(release_pages(block, pages));
return last; return last;
} }
else else
@ -286,8 +273,7 @@ Result<void> kfree(void* ptr)
if ((block->status & BLOCK_START_MEM) && (block->status & BLOCK_END_MEM)) if ((block->status & BLOCK_START_MEM) && (block->status & BLOCK_END_MEM))
{ {
heap.remove(block); heap.remove(block);
const usize pages = get_blocks_from_size(block->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE); TRY(release_pages(block, get_blocks_from_size(block->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE)));
TRY(release_pages(block, pages));
} }
return {}; return {};
@ -366,15 +352,12 @@ void dump_heap_usage()
{ {
if (is_block_free(block)) if (is_block_free(block))
{ {
kdbgln("- Available block (%p), of size %zu (%s%s)", (void*)block, block->full_size, kdbgln("- Available block, of size %zu", block->full_size);
block->status & BLOCK_START_MEM ? "b" : "-", block->status & BLOCK_END_MEM ? "e" : "-");
alloc_total += block->full_size + sizeof(HeapBlock); alloc_total += block->full_size + sizeof(HeapBlock);
} }
else else
{ {
kdbgln("- Used block (%p), of size %zu, of which %zu bytes are being used (%s%s)", (void*)block, kdbgln("- Used block, of size %zu, of which %zu bytes are being used", block->full_size, block->req_size);
block->full_size, block->req_size, block->status & BLOCK_START_MEM ? "b" : "-",
block->status & BLOCK_END_MEM ? "e" : "-");
alloc_total += block->full_size + sizeof(HeapBlock); alloc_total += block->full_size + sizeof(HeapBlock);
alloc_used += block->req_size; alloc_used += block->req_size;
} }

View File

@ -49,14 +49,12 @@ template <typename T> class LinkedListNode
{ {
end_node->m_next_node = this; end_node->m_next_node = this;
this->m_last_node = end_node; this->m_last_node = end_node;
this->m_next_node = nullptr;
} }
void prepend_to_list(SelfType* start_node) void prepend_to_list(SelfType* start_node)
{ {
start_node->m_last_node = this; start_node->m_last_node = this;
this->m_next_node = start_node; this->m_next_node = start_node;
this->m_last_node = nullptr;
} }
friend class LinkedList<T>; friend class LinkedList<T>;
@ -106,8 +104,6 @@ template <typename T> class LinkedList
if (m_end_node == base_node) m_end_node = new_node; if (m_end_node == base_node) m_end_node = new_node;
if (base_node->get_next()) base_node->get_next()->set_last(new_node);
new_node->set_next(base_node->get_next()); new_node->set_next(base_node->get_next());
base_node->set_next(new_node); base_node->set_next(new_node);
new_node->set_last(base_node); new_node->set_last(base_node);