From 909bb3366618e98852bbc8d18f06e95e56c8b6a3 Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Fri, 14 Jan 2022 09:06:51 +0100 Subject: [PATCH] Refactor --- kernel/kernel.c | 2 +- kernel/memory.c | 14 ++++++++++---- lib/conv.c | 5 +++-- lib/types.h | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/kernel/kernel.c b/kernel/kernel.c index 5343d61..be39184 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -25,7 +25,7 @@ void init() { println("0x1000", DEFAULT_COLOR); */ - char* malloctest = pm_malloc(2); // allocate two blocks + char* malloctest = pm_malloc(1); // allocate two blocks diff --git a/kernel/memory.c b/kernel/memory.c index 117ec97..a38ee8c 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -1,5 +1,6 @@ #include "memory.h" #include "../drivers/vga.h" +#include "../lib/conv.h" // https://wiki.osdev.org/Page_Frame_Allocation // page = block @@ -60,9 +61,10 @@ bool check_block_range(uint start, uint end) { uint idx; for(idx = start; idx <= end; idx++) { - if( CHECK_BITMAP(bitmap, idx) != BM_FREE ) + if( CHECK_BITMAP(bitmap, idx) != BM_FREE ) { allowed = false; break; + } } return allowed; @@ -74,11 +76,12 @@ int find_free(uint block_count) { // Loop through bitmap starting at last_block for( uint lower = last_block; lower < MAX_BLOCK_COUNT - block_count; lower++ ) { - bool range_is_free = check_block_range(lower, lower + block_count - 1); + bool range_is_free = check_block_range(lower, lower + block_count); - if(range_is_free) // if range is free + if(range_is_free) { // if range is free lowerb = (int)lower; break; // then stop searching + } } return lowerb; // return the lower block index @@ -111,11 +114,14 @@ pointer pm_malloc(uint block_count) { // find free block range and get lower offset int lower; lower = find_free(block_count); + println("lower: ", DEFAULT_COLOR); + println((lower+46), 0xfc); - if( lower < 0 ) + if( lower < 0 ) { println("malloc: OUT OF MEMORY :(", URGENT_COLOR); // do some out-of-memory interupt return 0x0; + } // allocate those blocks uint i; diff --git a/lib/conv.c b/lib/conv.c index 2ba09b2..8b5d82d 100644 --- a/lib/conv.c +++ b/lib/conv.c @@ -8,10 +8,11 @@ char* int_to_str(int i, char* buf) { *(buf+len) = '\0'; // add a "end-of-string" at the end int j; - for(j = 0; j < len; j++) // iterate over each digit and assign it to the buffer + for(j = 0; j < len; j++) { // iterate over each digit and assign it to the buffer // super dangerous memory write - println("char!", DEFAULT_COLOR); *(buf+j) = (char)(ndigit(num, len-1-j) + ASCII_OFFSET); // apply the ascii offset so that i becomes a char + println(*(buf+j), 0xc0); + } return buf; } diff --git a/lib/types.h b/lib/types.h index d79be95..f7bde5d 100644 --- a/lib/types.h +++ b/lib/types.h @@ -1,6 +1,6 @@ typedef unsigned int uint; typedef unsigned long ulong; -typedef unsigned long* pointer; +typedef char* pointer; typedef int bool; #define true 1 #define false 0