(memory): updating kcoalesce only merges contiguous memory locations.

This commit is contained in:
Liam Kerr 2026-02-11 02:38:43 +00:00
parent 3c7b927abf
commit 820163395e
2 changed files with 23 additions and 10 deletions

View file

@ -23,7 +23,7 @@ void page_init()
{
kprint("Initialising page allocator...");
uintptr_t start = ((uintptr_t)_heap_start + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
uintptr_t start = align_up((size_t)_heap_start, PAGE_SIZE);
uintptr_t end = 0x88000000; // Default QEMU RAM limit
for (uintptr_t addr = start; addr + PAGE_SIZE <= end; addr += PAGE_SIZE)
@ -32,9 +32,9 @@ void page_init()
}
kputs("OK");
// test_memory_integrity();
// test_memory_alignment();
// test_memory_stress();
test_memory_integrity();
test_memory_alignment();
test_memory_stress();
// page_free((void *)23);
}
@ -174,11 +174,6 @@ void kcoalesce(HeapHeader *header)
{
if (!header || !header->is_free)
return;
// jump backward
while (header->prev && header->prev->is_free)
{
header = header->prev;
}
// merge forward
while (header->next && header->next->is_free)
{
@ -197,6 +192,15 @@ void kcoalesce(HeapHeader *header)
break;
}
}
if (header->prev && header->prev->is_free)
{
uintptr_t prev_end = (uintptr_t)header->prev + sizeof(HeapHeader) + header->prev->size;
if (prev_end == (uintptr_t)header)
{
kcoalesce(header->prev);
}
}
}
void test_memory_integrity()
@ -204,10 +208,16 @@ void test_memory_integrity()
kprint("Running Integrity Test...\n");
uint64_t *a = (uint64_t *)kmalloc(16);
uint64_t *b = (uint64_t *)kmalloc(16);
uint64_t *c = (uint64_t *)kmalloc(512);
uint64_t *d = (uint64_t *)kmalloc(2048);
uint64_t *e = (uint64_t *)kmalloc(1024);
uint64_t *f = (uint64_t *)kmalloc(2048);
*a = 0x1122334455667788;
*b = 0x99AABBCCDDEEFF00;
heap_stats();
if (*a == 0x1122334455667788)
{
kprint("Integrity Pass!\n");
@ -217,6 +227,8 @@ void test_memory_integrity()
kprint("CORRUPTION DETECTED!\n");
}
kfree(a);
kfree(f);
heap_stats();
kfree(b);
}

View file

@ -14,7 +14,8 @@ typedef struct HeapHeader
int is_free;
struct HeapHeader *next;
struct HeapHeader *prev;
} HeapHeader;
uint64_t _padding;
} __attribute__((aligned(16))) HeapHeader;
void zero_bss();
int is_aligned_to(size_t value, size_t size);