From e0e08a12d4de86b558674fb9f90ad0b0204c26f0 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Thu, 26 Dec 2019 15:50:21 -0500 Subject: working memory manager --- src/kernel/mem.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'src/kernel/mem.c') diff --git a/src/kernel/mem.c b/src/kernel/mem.c index 636c23e..99586d9 100644 --- a/src/kernel/mem.c +++ b/src/kernel/mem.c @@ -19,6 +19,9 @@ OF THIS SOFTWARE. #include "mem.h" +uint32_t last_byte = 0; +uint32_t last_mask = 0x00000001; + void *allocate_block(uint32_t size, uint16_t proc_n) { struct mmap_entry *i = mmap_start; while (i->whose || (i->length < size)) @@ -33,8 +36,28 @@ void *allocate_block(uint32_t size, uint16_t proc_n) { uint32_t remaining = i->length - size; i->whose = proc_n; i->length = size; - struct mmap_entry *new; - //TODO: ALLOCATE NEW + + uint32_t byte = last_byte; + uint32_t mask = last_mask; + do { + if (!(mask <<= 1)) { + mask = 0x00000001; + if (++byte == MMAP_SIZE) + byte = 0; + } + if ((byte == last_byte) && (mask == last_mask)) + return 0; + } while (mmap_bitmap[byte] & mask); + last_byte = byte; + last_mask = mask; + mmap_bitmap[byte] |= mask; + uint8_t bit = 0; + while (!(mask & 0x00000001)) { + ++bit; + mask >>= 1; + } + + struct mmap_entry *new = mmap_bss + (byte << 5) + bit; new->base = i->base + size; new->length = remaining; new->whose = FREE; @@ -57,6 +80,7 @@ void deallocate_block(void *start) { if (!find_after->whose) { find->length += find_after->length; find->next = find_after->next; + unmark_entry(find_after); } break; } @@ -66,6 +90,7 @@ void deallocate_block(void *start) { if (!find_before->whose) { find_before->length += find->length; find_before->next = find->next; + unmark_entry(find); } break; } -- cgit v1.2.3