(memory): updating kcoalesce only merges contiguous memory locations.
This commit is contained in:
parent
3c7b927abf
commit
820163395e
2 changed files with 23 additions and 10 deletions
|
|
@ -23,7 +23,7 @@ void page_init()
|
||||||
{
|
{
|
||||||
kprint("Initialising page allocator...");
|
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
|
uintptr_t end = 0x88000000; // Default QEMU RAM limit
|
||||||
|
|
||||||
for (uintptr_t addr = start; addr + PAGE_SIZE <= end; addr += PAGE_SIZE)
|
for (uintptr_t addr = start; addr + PAGE_SIZE <= end; addr += PAGE_SIZE)
|
||||||
|
|
@ -32,9 +32,9 @@ void page_init()
|
||||||
}
|
}
|
||||||
kputs("OK");
|
kputs("OK");
|
||||||
|
|
||||||
// test_memory_integrity();
|
test_memory_integrity();
|
||||||
// test_memory_alignment();
|
test_memory_alignment();
|
||||||
// test_memory_stress();
|
test_memory_stress();
|
||||||
// page_free((void *)23);
|
// page_free((void *)23);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -174,11 +174,6 @@ void kcoalesce(HeapHeader *header)
|
||||||
{
|
{
|
||||||
if (!header || !header->is_free)
|
if (!header || !header->is_free)
|
||||||
return;
|
return;
|
||||||
// jump backward
|
|
||||||
while (header->prev && header->prev->is_free)
|
|
||||||
{
|
|
||||||
header = header->prev;
|
|
||||||
}
|
|
||||||
// merge forward
|
// merge forward
|
||||||
while (header->next && header->next->is_free)
|
while (header->next && header->next->is_free)
|
||||||
{
|
{
|
||||||
|
|
@ -197,6 +192,15 @@ void kcoalesce(HeapHeader *header)
|
||||||
break;
|
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()
|
void test_memory_integrity()
|
||||||
|
|
@ -204,10 +208,16 @@ void test_memory_integrity()
|
||||||
kprint("Running Integrity Test...\n");
|
kprint("Running Integrity Test...\n");
|
||||||
uint64_t *a = (uint64_t *)kmalloc(16);
|
uint64_t *a = (uint64_t *)kmalloc(16);
|
||||||
uint64_t *b = (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;
|
*a = 0x1122334455667788;
|
||||||
*b = 0x99AABBCCDDEEFF00;
|
*b = 0x99AABBCCDDEEFF00;
|
||||||
|
|
||||||
|
heap_stats();
|
||||||
|
|
||||||
if (*a == 0x1122334455667788)
|
if (*a == 0x1122334455667788)
|
||||||
{
|
{
|
||||||
kprint("Integrity Pass!\n");
|
kprint("Integrity Pass!\n");
|
||||||
|
|
@ -217,6 +227,8 @@ void test_memory_integrity()
|
||||||
kprint("CORRUPTION DETECTED!\n");
|
kprint("CORRUPTION DETECTED!\n");
|
||||||
}
|
}
|
||||||
kfree(a);
|
kfree(a);
|
||||||
|
kfree(f);
|
||||||
|
heap_stats();
|
||||||
kfree(b);
|
kfree(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ typedef struct HeapHeader
|
||||||
int is_free;
|
int is_free;
|
||||||
struct HeapHeader *next;
|
struct HeapHeader *next;
|
||||||
struct HeapHeader *prev;
|
struct HeapHeader *prev;
|
||||||
} HeapHeader;
|
uint64_t _padding;
|
||||||
|
} __attribute__((aligned(16))) HeapHeader;
|
||||||
|
|
||||||
void zero_bss();
|
void zero_bss();
|
||||||
int is_aligned_to(size_t value, size_t size);
|
int is_aligned_to(size_t value, size_t size);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue