diff options
author | Your Name <you@example.com> | 2020-06-27 04:09:45 +0200 |
---|---|---|
committer | Your Name <you@example.com> | 2020-06-27 04:09:45 +0200 |
commit | 2c1415ed1dda166f8ca9fa9a721472e93320269e (patch) | |
tree | 96a2b88a73aed40a6816871ad446dfdb43a878a3 | |
parent | Base: added an end symbol to the linker to signify the end of mem used (diff) | |
download | pOS-2c1415ed1dda166f8ca9fa9a721472e93320269e.tar.gz pOS-2c1415ed1dda166f8ca9fa9a721472e93320269e.tar.bz2 pOS-2c1415ed1dda166f8ca9fa9a721472e93320269e.zip |
Base: added a basic pmm
malloc(), calloc(), realloc() and free() kinda implemented.
-rw-r--r-- | src/pOS/arch/x86/kernel/drivers.cpp | 2 | ||||
-rw-r--r-- | src/pOS/arch/x86/kernel/physm.cpp | 131 | ||||
-rw-r--r-- | src/pOS/arch/x86/libc/stdlib/memory.cpp | 12 | ||||
-rw-r--r-- | src/pOS/include/kernel/kernel.h | 1 | ||||
-rw-r--r-- | src/pOS/include/kernel/pOS.h | 3 | ||||
-rw-r--r-- | src/pOS/include/kernel/physm.h | 30 | ||||
-rw-r--r-- | src/pOS/include/libc/stdlib.h | 2 |
7 files changed, 175 insertions, 6 deletions
diff --git a/src/pOS/arch/x86/kernel/drivers.cpp b/src/pOS/arch/x86/kernel/drivers.cpp index 08e5e6f..f8d469e 100644 --- a/src/pOS/arch/x86/kernel/drivers.cpp +++ b/src/pOS/arch/x86/kernel/drivers.cpp @@ -5,7 +5,7 @@ Driver* Drivers::loaded_drivers[1]; void Drivers::add_drivers(void) { - #ifdef DRIVER_EXAMPLE + #ifdef DRIVER_KEYBOARD ADD_DRIVER(KeyboardDriver); #endif } diff --git a/src/pOS/arch/x86/kernel/physm.cpp b/src/pOS/arch/x86/kernel/physm.cpp index b3e1705..e76116f 100644 --- a/src/pOS/arch/x86/kernel/physm.cpp +++ b/src/pOS/arch/x86/kernel/physm.cpp @@ -1,21 +1,148 @@ #include <kernel/physm.h> +Block PhysM::blocks[MEM_SIZE / BLOCK_SIZE]; +uint32_t PhysM::total_blocks; +void* PhysM::memory; + +/* very naive implementaiton, temp */ + +int PhysM::print_debug(void) +{ + printf("-------------------------\n"); + printf("mem starts at: %p\n", (void*)memory); + for(size_t i = 0; i < total_blocks; i++) + { + printf("block: %d - ", i); + printf("used: %d - ", blocks[i].used); + printf("blockid: %d - ", blocks[i].allocate_id); + printf("block at: %p\n", (void*)blocks[i].memory); + } + printf("++++++++++++++++++++++++++\n"); + + return 0; +} + int PhysM::init(void) { + total_blocks = MEM_SIZE / BLOCK_SIZE; + memory = memset((void*)(&end), 0, total_blocks * BLOCK_SIZE); + + /* Init memory in blocks */ + for(size_t i = 0; i < total_blocks; i++) + { + blocks[i].memory = (char*)memory + (i * BLOCK_SIZE); + } return 0; } -void* PhysM::malloc(size_t size) +void* PhysM::malloc(size_t size, bool calloc) { if(!size) return NULL; - return NULL; + uint32_t num = size / BLOCK_SIZE; + if(num * BLOCK_SIZE < size) + num++; + return allocate_blocks(num, calloc); +} + +void* PhysM::realloc(void* oldmem, size_t size) +{ + void* newmem = malloc(size, false); + size_t oldsize = get_blocks_num(oldmem) * BLOCK_SIZE; + + memcpy(newmem, oldmem, oldsize); + free(oldmem); + + return newmem; } void PhysM::free(void* ptr) { if(!ptr) return; + + Block& block = find_block_from_memory(ptr); + free_all_blocks_from(block); +} + +void* PhysM::allocate_blocks(uint32_t num, bool calloc) +{ + int first_block_index = find_best_block(num); + if(first_block_index == -1) + return NULL; + + uint32_t id = Timer::get_ms_from_boot(); + + for(unsigned int i = first_block_index; i < (first_block_index + num); i++) + { + Block* block = &blocks[i]; + block->used = 1; + block->allocate_id = id; + if(calloc) + memset(block->memory, 0, BLOCK_SIZE); + } + + return blocks[first_block_index].memory; +} + +int PhysM::free_all_blocks_from(Block& block) +{ + uint32_t id = block.allocate_id; + for(uint32_t i = 0; i < total_blocks; i++) + { + Block* blk = &blocks[i]; + if(blk->allocate_id == id) + blk->used = 0; + } + + return 0; +} + +Block& PhysM::find_block_from_memory(void* memory_from_block) +{ + int index = ((char*)memory_from_block - (char*)memory) / BLOCK_SIZE;//calculate block num from addr + //printf("freeing block %d\n", index); + return blocks[index]; +} + +bool PhysM::is_suitable_block(uint32_t block_index, uint32_t block_num) +{ + //printf("finding mem for %d blocks\n", block_num); + for(size_t i = 0; i < block_num; i++) + { + Block* examining_block = &blocks[block_index++]; + if(block_index > total_blocks || examining_block->used) + return false; + } + + return true; +} + +int PhysM::find_best_block(uint32_t num) +{ + for(size_t i = 0; i < total_blocks; i++) + { + if(is_suitable_block(i, num)) + return i; + } + + printf("Run out of heap memory!\n"); + return -1; +} + +int PhysM::get_blocks_num(void* example_block_mem_ptr) +{ + int num = 0; + Block& block = find_block_from_memory(example_block_mem_ptr); + uint32_t id = block.allocate_id; + + for(size_t i = 0; i < total_blocks; i++) + { + if(blocks[i].allocate_id == id) + num++; + } + + return num; } diff --git a/src/pOS/arch/x86/libc/stdlib/memory.cpp b/src/pOS/arch/x86/libc/stdlib/memory.cpp index fec9ef8..d56be5e 100644 --- a/src/pOS/arch/x86/libc/stdlib/memory.cpp +++ b/src/pOS/arch/x86/libc/stdlib/memory.cpp @@ -2,7 +2,17 @@ void *malloc(size_t size) { - return PhysM::malloc(size); + return PhysM::malloc(size, false); +} + +void *calloc(size_t size) +{ + return PhysM::malloc(size, true); +} + +void* realloc(void* ptr, size_t size) +{ + return PhysM::realloc(ptr, size); } void free(void* ptr) diff --git a/src/pOS/include/kernel/kernel.h b/src/pOS/include/kernel/kernel.h index 68ca24f..e85d49f 100644 --- a/src/pOS/include/kernel/kernel.h +++ b/src/pOS/include/kernel/kernel.h @@ -3,6 +3,7 @@ #include <stdio.h> #include <unistd.h> +#include <string.h> #include <math.h> #include <assert.h> diff --git a/src/pOS/include/kernel/pOS.h b/src/pOS/include/kernel/pOS.h index 076bbde..b5de63d 100644 --- a/src/pOS/include/kernel/pOS.h +++ b/src/pOS/include/kernel/pOS.h @@ -8,9 +8,10 @@ #define KB_US #define TAB_INDENTATION 4 +#define MEM_SIZE 1 * 1024 * 1024 //#define TIME_FORMAT_12H -#define DRIVER_EXAMPLE +#define DRIVER_KEYBOARD #endif diff --git a/src/pOS/include/kernel/physm.h b/src/pOS/include/kernel/physm.h index a7b3292..a833a29 100644 --- a/src/pOS/include/kernel/physm.h +++ b/src/pOS/include/kernel/physm.h @@ -2,13 +2,41 @@ #define _PHYSM_H_ #include <stddef.h> +#include <stdint.h> +#include <string.h> +#include <stdio.h> +#include <kernel/timer.h> + +#define BLOCK_SIZE 20 + +extern uint32_t end; + +struct Block +{ + uint32_t allocate_id; + int used; + void* memory; +}; class PhysM { public: static int init(void); - static void *malloc(size_t size); + static void* malloc(size_t size, bool calloc); + static void* realloc(void* oldmem, size_t size); static void free(void* ptr); + static int print_debug(void); +private: + static void* allocate_blocks(uint32_t num, bool calloc); + static int free_all_blocks_from(Block& block); + static Block& find_block_from_memory(void* memory_from_block); + static bool is_suitable_block(uint32_t block_index, uint32_t block_num); + static int find_best_block(uint32_t num); + static int get_blocks_num(void* example_block_mem_ptr); + + static Block blocks[MEM_SIZE / BLOCK_SIZE]; + static uint32_t total_blocks; + static void* memory; }; #endif diff --git a/src/pOS/include/libc/stdlib.h b/src/pOS/include/libc/stdlib.h index 86ba7a6..4e4728c 100644 --- a/src/pOS/include/libc/stdlib.h +++ b/src/pOS/include/libc/stdlib.h @@ -7,6 +7,8 @@ #include <kernel/physm.h> void *malloc(size_t size); +void *calloc(size_t size); +void* realloc(void* ptr, size_t size); void free(void* ptr); int itoa(int i, char* buf, size_t len); |