diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 88f261f..7d5d31e 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -12,6 +12,8 @@ drawLogo(); initMemoryAllocation(0x100000); // initializing stacks after the kernel seems to not work :( otherwise, _kernel_end should be passed here initOSTasks(); + printf("malloc 0x100: %x\n", malloc(0x100)); + printf("malloc 0x100: %x\n", malloc(0x100)); printf("Switching to otherTask... \n"); yields(); printf("Returned to mainTask!\n"); diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 88f261f..7d5d31e 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -12,6 +12,8 @@ drawLogo(); initMemoryAllocation(0x100000); // initializing stacks after the kernel seems to not work :( otherwise, _kernel_end should be passed here initOSTasks(); + printf("malloc 0x100: %x\n", malloc(0x100)); + printf("malloc 0x100: %x\n", malloc(0x100)); printf("Switching to otherTask... \n"); yields(); printf("Returned to mainTask!\n"); diff --git a/src/kernel/lib/memory/alloc.c b/src/kernel/lib/memory/alloc.c index 1c4d023..c4de5da 100644 --- a/src/kernel/lib/memory/alloc.c +++ b/src/kernel/lib/memory/alloc.c @@ -2,14 +2,28 @@ #include #include -uint32_t currentPosition; +MemoryBlock* firstBlock; void initMemoryAllocation(uint32_t kernelEnd) { - currentPosition = kernelEnd; + firstBlock = (MemoryBlock*) kernelEnd; + firstBlock->next = 0x00; + firstBlock->last = 0x00; + firstBlock->state = FREE; + firstBlock->size = -1; } void* malloc(uint32_t size) { - uint32_t result = currentPosition; - currentPosition += size; - return (void*) result; + MemoryBlock* current = firstBlock; + while (current->next != 0x00) { + current = current->next; + } + MemoryBlock* next = (MemoryBlock*) ((void*) (current + 1) + size); + next->last = current; + next->state = FREE; + next->next = 0x00; + next->size = -1; + current->next = next; + current->size = size; + current->state = IN_USE; + return (void*) current; } \ No newline at end of file diff --git a/src/kernel/kernel.c b/src/kernel/kernel.c index 88f261f..7d5d31e 100644 --- a/src/kernel/kernel.c +++ b/src/kernel/kernel.c @@ -12,6 +12,8 @@ drawLogo(); initMemoryAllocation(0x100000); // initializing stacks after the kernel seems to not work :( otherwise, _kernel_end should be passed here initOSTasks(); + printf("malloc 0x100: %x\n", malloc(0x100)); + printf("malloc 0x100: %x\n", malloc(0x100)); printf("Switching to otherTask... \n"); yields(); printf("Returned to mainTask!\n"); diff --git a/src/kernel/lib/memory/alloc.c b/src/kernel/lib/memory/alloc.c index 1c4d023..c4de5da 100644 --- a/src/kernel/lib/memory/alloc.c +++ b/src/kernel/lib/memory/alloc.c @@ -2,14 +2,28 @@ #include #include -uint32_t currentPosition; +MemoryBlock* firstBlock; void initMemoryAllocation(uint32_t kernelEnd) { - currentPosition = kernelEnd; + firstBlock = (MemoryBlock*) kernelEnd; + firstBlock->next = 0x00; + firstBlock->last = 0x00; + firstBlock->state = FREE; + firstBlock->size = -1; } void* malloc(uint32_t size) { - uint32_t result = currentPosition; - currentPosition += size; - return (void*) result; + MemoryBlock* current = firstBlock; + while (current->next != 0x00) { + current = current->next; + } + MemoryBlock* next = (MemoryBlock*) ((void*) (current + 1) + size); + next->last = current; + next->state = FREE; + next->next = 0x00; + next->size = -1; + current->next = next; + current->size = size; + current->state = IN_USE; + return (void*) current; } \ No newline at end of file diff --git a/src/kernel/lib/memory/alloc.h b/src/kernel/lib/memory/alloc.h index bd195ae..3eba3b0 100644 --- a/src/kernel/lib/memory/alloc.h +++ b/src/kernel/lib/memory/alloc.h @@ -3,6 +3,18 @@ #include +typedef enum MemoryState { + FREE = 0, + IN_USE = 1 +} MemoryState; + +typedef struct MemoryBlock { + struct MemoryBlock* last; + struct MemoryBlock* next; + MemoryState state; + uint32_t size; +} MemoryBlock; + extern void initMemoryAllocation(uint32_t kernelEnd); extern void* malloc(uint32_t size);