From 2135068a2300d73688b4f7cd2bf52d2c26b71d77 Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Mon, 20 Sep 2021 10:31:11 -0300 Subject: [PATCH] Add naive memory management Co-authored-by: Juan Barmasch Co-authored-by: Ezequiel Bellver --- .gdbinit | 4 +- .gitignore | 8 ++++ .vscode/settings.json | 26 ----------- Image/Makefile | 1 + Kernel/include/memManager.h | 12 +++++ Kernel/memManager.c | 89 +++++++++++++++++++++++++++++++++++++ 6 files changed, 113 insertions(+), 27 deletions(-) delete mode 100644 .vscode/settings.json create mode 100644 Kernel/include/memManager.h create mode 100644 Kernel/memManager.c diff --git a/.gdbinit b/.gdbinit index 577d57c..5b54b0e 100644 --- a/.gdbinit +++ b/.gdbinit @@ -1,5 +1,7 @@ # target remote 192.168.1.110:1234 -target remote 172.30.100.135:1234 +set auto-load safe-path . + +target remote 192.168.2.62:1234 add-symbol-file ~/Kernel/kernel.elf 0x100000 add-symbol-file ~/Userland/0000-sampleCodeModule.elf 0x400000 diff --git a/.gitignore b/.gitignore index 0a6c5b0..ff8c224 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,10 @@ +.bash_history +.gdb_history +.viminfo +.config/* +index.html +.bashrc + #Binary Files *.bin *.sys @@ -6,6 +13,7 @@ *.img *.qcow2 *.vmdk +*.elf #Object files *.o \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 5aa68b8..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "files.associations": { - "interrupts.h": "c", - "time.h": "c", - "naiveconsole.h": "c", - "keyboard.h": "c", - "video.h": "c", - "lib.h": "c", - "libc.h": "c", - "cstdio": "c", - "shell.h": "c", - "defs.h": "c", - "idtloader.h": "c", - "exit.h": "c", - "help.h": "c", - "cstdlib": "c", - "inforeg.h": "c", - "cpuid.h": "c", - "cpu_id.h": "c", - "switch.h": "c", - "stdint.h": "c", - "pcb.h": "c", - "systemcalls.h": "c", - "quadratic.h": "c" - } -} \ No newline at end of file diff --git a/Image/Makefile b/Image/Makefile index f0ea1d2..6115f7b 100644 --- a/Image/Makefile +++ b/Image/Makefile @@ -29,6 +29,7 @@ $(VMDK): $(IMG) $(QCOW2): $(IMG) qemu-img convert -f raw -O qcow2 $(IMG) $(QCOW2) + chmod o+w $(QCOW2) clean: rm -rf $(IMG) $(VMDK) $(QCOW2) *.bin diff --git a/Kernel/include/memManager.h b/Kernel/include/memManager.h new file mode 100644 index 0000000..c3e787e --- /dev/null +++ b/Kernel/include/memManager.h @@ -0,0 +1,12 @@ +#ifndef MEM_MANAGER_H +#define MEM_MANAGER_H + +#include + +typedef struct MemoryManagerCDT * MemoryManagerADT; + +MemoryManagerADT createMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory); + +void *allocMemory(MemoryManagerADT const restrict memoryManager, const size_t memoryToAllocate); + +#endif \ No newline at end of file diff --git a/Kernel/memManager.c b/Kernel/memManager.c new file mode 100644 index 0000000..b4cb7e0 --- /dev/null +++ b/Kernel/memManager.c @@ -0,0 +1,89 @@ +#include "include/memManager.h" +#include +#include +#include + +#define MANAGED_MEMORY_SIZE 20480 + +char mem[MANAGED_MEMORY_SIZE]; +char mem2[MANAGED_MEMORY_SIZE]; + +typedef struct MemoryManagerCDT { + char *nextAddress; + char *lastAddress; + char *initialAddress; +} MemoryManagerCDT; + +MemoryManagerADT createMemoryManager(void *const restrict memoryForMemoryManager, void *const restrict managedMemory) { + MemoryManagerADT memoryManager = (MemoryManagerADT) memoryForMemoryManager; + memoryManager->nextAddress = managedMemory; + char * aux = managedMemory; + memoryManager->lastAddress = aux - 1; + memoryManager->initialAddress = managedMemory; + + return memoryManager; +} + +void *allocMemory(MemoryManagerADT const restrict memoryManager, const size_t memoryToAllocate) { + char *allocation = memoryManager->nextAddress; + if (memoryToAllocate + memoryManager->nextAddress > memoryManager->initialAddress + MANAGED_MEMORY_SIZE){ + return NULL; + } + + memoryManager->nextAddress += memoryToAllocate; + if (!(memoryManager->nextAddress -1 == (memoryManager->lastAddress += memoryToAllocate))) + return NULL; + + return (void *) allocation; +} + +int main() { + static MemoryManagerADT memoryManager; + static MemoryManagerADT memoryManager2; + + void * memoryForMemoryManager = (void *) 0x700000; + // void * memoryForMemoryManager = malloc(sizeof(void *)); + if (memoryForMemoryManager == NULL) { + return 1; + } + + void *managedMemory = (void *) 0x700016;// malloc(MANAGED_MEMORY_SIZE); + if (managedMemory == NULL) { + return 1; + } + + + void * memoryForMemoryManager2 = (void *) 0x700008; + // void * memoryForMemoryManager2 = malloc(sizeof(void *)); + memoryManager = createMemoryManager(memoryForMemoryManager, managedMemory); + + void * alloc1 = allocMemory(memoryManager, 100); + void * alloc2 = allocMemory(memoryManager, 200); + void * alloc3 = allocMemory(memoryManager, 300); + + memset(alloc1, 1, 100); + memset(alloc2, 2, 200); + memset(alloc3, 3, 300); + + for (int i = 0; i < 600; i++) { + if (i < 100) { + assert(*((char *) alloc1+i) == 1); + } else if (i < 300) { + assert(*((char *) alloc1+i) == 2); + } else if (i < 600) { + assert(*((char *) alloc1+i) == 3); + } + } + + managedMemory = (void *) (0x700016 + 600); + memoryManager2 = createMemoryManager(memoryForMemoryManager2, managedMemory); + + void * ptr; + while (ptr != NULL){ + ptr = allocMemory(memoryManager2, (rand() % 2000) + 1); + assert ((char *) memoryManager2->nextAddress >= mem2); + assert ((char *) memoryManager2->nextAddress <= mem2 + MANAGED_MEMORY_SIZE); + assert ((char *) memoryManager2->lastAddress >= mem2); + assert ((char *) memoryManager2->lastAddress <= mem2 + MANAGED_MEMORY_SIZE); + } +} \ No newline at end of file