kmalloc returns aligned memory

This commit is contained in:
2023-10-22 11:12:26 +02:00
parent 8c1d0ac81a
commit 609c45a71c
3 changed files with 15 additions and 2 deletions

View File

@@ -84,6 +84,15 @@ struct HeapEntry *split_entry(struct HeapEntry *what, size_t n) {
void *kmalloc(size_t n) {
assert(initialized);
if ((n & 0xFULL) != 0) {
size_t origN = n;
n += 15;
n &= ~0xFULL;
assert(n > origN);
}
assert((n & 0xFULL) == 0);
struct HeapEntry *res = NULL;
{
LockGuard l(kmem_lock);
@@ -182,6 +191,8 @@ void *kmalloc(size_t n) {
res->prev = NULL;
res->magic = KERN_HeapMagicTaken;
}
assert((((uintptr_t) res->data) & 0xFULL) == 0);
for (size_t i = 0; i < n; i++) res->data[i] = 0xFEU;
used.fetch_add(n);
return res->data;

View File

@@ -15,8 +15,8 @@ struct HeapEntry {
struct HeapEntry *next;
struct HeapEntry *prev;
uint64_t len;
char data[];
};
char data[] __attribute__((aligned(16)));
} __attribute__((packed, aligned(32)));
extern struct HeapEntry *KERN_HeapBegin;
extern uintptr_t KERN_HeapEnd;// Past the end

View File

@@ -182,6 +182,8 @@ struct Task *new_ktask(void (*fn)(), const char *name) {
strcpy(name, newt->name);
newt->frame.sp = ((((uintptr_t) newt->stack) + TASK_SS - 1) & (~0xFULL));// Ensure 16byte alignment
assert((newt->frame.sp & 0xFULL) == 0);
newt->frame.ip = (uint64_t) fn;
newt->frame.cs = GDTSEL(gdt_code);
newt->frame.ss = GDTSEL(gdt_data);