From 2b7b69e1ffd00aefa60d73a5af3bacc1c803a930 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Wed, 25 Dec 2019 11:49:06 -0500 Subject: more progress --- src/kernel/mem.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'src/kernel/mem.c') diff --git a/src/kernel/mem.c b/src/kernel/mem.c index 6544108..636c23e 100644 --- a/src/kernel/mem.c +++ b/src/kernel/mem.c @@ -20,9 +20,53 @@ OF THIS SOFTWARE. #include "mem.h" void *allocate_block(uint32_t size, uint16_t proc_n) { - //TODO + struct mmap_entry *i = mmap_start; + while (i->whose || (i->length < size)) + if (i->next) + i = i->next; + else + return (void *)0; + if (i->length == size) { + i->whose = proc_n; + return (void *)i->base; + } + uint32_t remaining = i->length - size; + i->whose = proc_n; + i->length = size; + struct mmap_entry *new; + //TODO: ALLOCATE NEW + new->base = i->base + size; + new->length = remaining; + new->whose = FREE; + new->next = i->next; + i->next = new; + return (void *)i->base; } void deallocate_block(void *start) { - //TODO + struct mmap_entry *find = mmap_start; + while (find->base != (uint32_t)start) + if (find->next) + find = find->next; + else + return; + find->whose = FREE; + + for (struct mmap_entry *find_after = mmap_start; find_after; find_after = find_after->next) + if (find_after->base == find->base + find->length) { + if (!find_after->whose) { + find->length += find_after->length; + find->next = find_after->next; + } + break; + } + + for (struct mmap_entry *find_before = mmap_start; find_before; find_before = find_before->next) + if (find->base == find_before->base + find_before->length) { + if (!find_before->whose) { + find_before->length += find->length; + find_before->next = find->next; + } + break; + } } \ No newline at end of file -- cgit v1.2.3