Compare commits

...

3 Commits

Author SHA1 Message Date
ed34009b50
Heap: Log more details of blocks
All checks were successful
continuous-integration/drone/push Build is passing
2022-12-26 16:17:24 +01:00
2af3997456
LinkedList: Fixed some boogs, LinkedList is now boog-free :) 2022-12-26 15:54:29 +01:00
1c70ab5a1a
Heap: Avoid combines with blocks outside a range 2022-12-26 15:20:56 +01:00
2 changed files with 26 additions and 5 deletions

View File

@ -125,8 +125,14 @@ 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;
@ -134,7 +140,8 @@ static Result<void> combine_forward(HeapBlock* block)
{ {
if (next->status & BLOCK_START_MEM) if (next->status & BLOCK_START_MEM)
{ {
TRY(release_pages(next, get_blocks_from_size(next->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE))); const usize pages = get_blocks_from_size(next->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE);
TRY(release_pages(next, pages));
return {}; return {};
} }
else else
@ -148,8 +155,13 @@ 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;
@ -157,7 +169,8 @@ static Result<HeapBlock*> combine_backward(HeapBlock* block)
{ {
if (block->status & BLOCK_START_MEM) if (block->status & BLOCK_START_MEM)
{ {
TRY(release_pages(block, get_blocks_from_size(block->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE))); const usize pages = get_blocks_from_size(block->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE);
TRY(release_pages(block, pages));
return last; return last;
} }
else else
@ -273,7 +286,8 @@ 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);
TRY(release_pages(block, get_blocks_from_size(block->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE))); const usize pages = get_blocks_from_size(block->full_size + sizeof(HeapBlock), ARCH_PAGE_SIZE);
TRY(release_pages(block, pages));
} }
return {}; return {};
@ -352,12 +366,15 @@ void dump_heap_usage()
{ {
if (is_block_free(block)) if (is_block_free(block))
{ {
kdbgln("- Available block, of size %zu", block->full_size); kdbgln("- Available block (%p), of size %zu (%s%s)", (void*)block, 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, of size %zu, of which %zu bytes are being used", block->full_size, block->req_size); kdbgln("- Used block (%p), of size %zu, of which %zu bytes are being used (%s%s)", (void*)block,
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,12 +49,14 @@ 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>;
@ -104,6 +106,8 @@ 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);