diff options
Diffstat (limited to 'src/kernel/mem.c')
-rw-r--r-- | src/kernel/mem.c | 48 |
1 files changed, 46 insertions, 2 deletions
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 |