Skip to content

Commit

Permalink
Changed the location of random function defines.
Browse files Browse the repository at this point in the history
  • Loading branch information
pradosh-arduino committed Jan 15, 2025
1 parent 6423554 commit e50401d
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 69 deletions.
10 changes: 9 additions & 1 deletion source/includes/debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ void debug_print(cstring msg);
*
* @param msg
*/
void debug_println(cstring msg);
void debug_println(cstring msg);

/**
* @brief printf implemented to debug.
*
* @param format
* @param ...
*/
void debug_printf(cstring format, ...);
10 changes: 9 additions & 1 deletion source/includes/strings2.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,12 @@ string strcat(string dest, cstring src);
*/
void remove_last_char(string str);

long strtol(const char *str, char **endptr, int base);
long strtol(const char *str, char **endptr, int base);

/**
* @brief Converts an uint to string
*
* @param num
* @return char*
*/
char* uint_to_string(unsigned int num);
43 changes: 43 additions & 0 deletions source/kernel/C/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
*
*/
#include <debugger.h>
#include <basics.h>
#include <stdarg.h>

/**
* @brief If there was possibility for the change in port, we can easily change the E9 Port.
Expand Down Expand Up @@ -50,4 +52,45 @@ void debug_println(cstring msg){
msg++;
}
debug_putc('\n');
}

/**
* @brief printf implemented to debug.
*
* @param format
* @param ...
*/
void debug_printf(cstring format, ...)
{
va_list argp;
va_start(argp, format);

while (*format != '\0') {
if (*format == '%') {
format++;
switch (*format)
{
case 'u':
debug_print(uint_to_string(va_arg(argp, size_t)));
break;

case 's':
debug_print(va_arg(argp, char*));
break;

case 'c':
debug_putc(va_arg(argp, char));
break;
}
} else {
if(*format == "\n") debug_putc('\n');
else if(*format == "\r") debug_putc('\r');
else if(*format == "\t") debug_putc('\t');
else debug_putc(*format);
}
format++;
}

debug_print("\n");
va_end(argp);
}
42 changes: 29 additions & 13 deletions source/kernel/C/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ void init_heap(int size) {
printf("Given Heap size (MB) : %d MiB", size/(1024*1024));
printf("Holder Heap size (MB) : %d MiB", sizeof(holder)/(1024*1024));
print(reset_color);
debug_print(yellow_color);
debug_printf("Given Heap size (Bytes) : %u Bytes", size);
debug_printf("Holder Heap size (Bytes) : %u Bytes", sizeof(holder));
debug_printf("Given Heap size (MB) : %u MiB", size/(1024*1024));
debug_printf("Holder Heap size (MB) : %u MiB", sizeof(holder)/(1024*1024));
print(reset_color);
done("Completed successfully!", __FILE__);
}

Expand All @@ -44,35 +50,45 @@ void init_heap(int size) {
* @param size The size of memory to allocate.
* @return A pointer to the allocated memory, or null if allocation fails.
*/
void* malloc(size_t size) {
if (size == 0) return null;
void* malloc(size_t size) {
if (size == 0) {
return null;
}

// Find a free block that is large enough to hold the requested memory.
int64 holder[size/sizeof(int64)];
heap_block* prev = (heap_block*)holder;
heap_block* prev = null;
heap_block* curr = free_list;
while (curr != null) {

while (curr != NULL) {
if (curr->size >= size) {
if (prev != null) {
// Remove the current block from the free list
if (prev != NULL) {
prev->next = curr->next;
} else {
free_list = curr->next;
}

// done("Found a free pointer! returning the pointer back...", __FILE__);
// Return a pointer to the allocated memory (skip the Block header).
return ((char*)curr) + sizeof(heap_block);
}else{
// If the block is significantly larger, split it
if (curr->size - size >= sizeof(heap_block)) {
heap_block* new_block = (heap_block*)((char*)curr + size);
new_block->size = curr->size - size;
new_block->next = free_list;
free_list = new_block;
}

debug_printf("Pointer location -> %u", (uint32_t)((char*)curr + sizeof(heap_block)));
return ((char*)curr) + sizeof(heap_block);
} else {
warn("Block size is too small! Checking next block...", __FILE__);
}

prev = curr;
curr = curr->next;
}

error("No suitable block found for allocation", __FILE__);
sleep(5);
// meltdown_screen("No suitable block found for allocation of heap.", __FILE__, __LINE__, 0x0);
// hcf(); // it is better to handle it here rather than leaving it.
meltdown_screen("No suitable block found for allocation of heap.", __FILE__, __LINE__, 0x0);
hcf(); // it is better to handle it here rather than leaving it.
return null;
}

Expand Down
61 changes: 7 additions & 54 deletions source/kernel/C/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void main(void) {
// Fetch the first framebuffer.
framebuffer = framebuffer_request.response->framebuffers[0];

init_heap(3 MiB);
init_heap(10 MiB);

ft_ctx = flanterm_fb_simple_init(
framebuffer->address, framebuffer->width, framebuffer->height, framebuffer->pitch
Expand Down Expand Up @@ -287,7 +287,7 @@ void main(void) {
warn("INSUFFICIENT MEMORY TO PROCEED WITH RE-INITIALIZATION HEAP!", __FILE__);
} else{
// Re-initializing heap with vast memory.
init_heap(memory.usable / 2);
// init_heap(memory.usable / 2);
}

printf("Total CPU(s): %d", smp_request.response->cpu_count);
Expand Down Expand Up @@ -385,6 +385,11 @@ void main(void) {
// print("\x1b[2J"); // Clears screen
// print("\x1b[H"); // Resets Cursor to 0, 0

// int* test1 = malloc(sizeof(int));
// int* test2 = malloc(sizeof(int));
// int* test3 = malloc(sizeof(int));
// int* test4 = malloc(sizeof(int));

int failed_attempts = 0;

// glCreateContext();
Expand Down Expand Up @@ -438,58 +443,6 @@ void print(cstring msg){
flanterm_write(ft_ctx, msg, strlen_(msg));
}

char* uint_to_string(unsigned int num) {
if (num == 0) {
return "0";
}

static char buf[21];
buf[20] = '\0';
int i = 20;

while (num > 0) {
buf[--i] = (num % 10) + '0';
num /= 10;
}

return &buf[i];
}

void debug_printf(cstring format, ...)
{
va_list argp;
va_start(argp, format);

while (*format != '\0') {
if (*format == '%') {
format++;
switch (*format)
{
case 'u':
debug_print(uint_to_string(va_arg(argp, size_t)));
break;

case 's':
debug_print(va_arg(argp, char*));
break;

case 'c':
debug_putc(va_arg(argp, char));
break;
}
} else {
if(*format == "\n") debug_putc('\n');
else if(*format == "\r") debug_putc('\r');
else if(*format == "\t") debug_putc('\t');
else debug_putc(*format);
}
format++;
}

debug_print("\n");
va_end(argp);
}

/**
* @brief The basic put char function.
*
Expand Down
23 changes: 23 additions & 0 deletions source/kernel/C/strings.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,27 @@ char* leading_trailing_trim(const char *str) {
trimmed[end - start + 1] = '\0';

return trimmed;
}

/**
* @brief Converts an uint to string
*
* @param num
* @return char*
*/
char* uint_to_string(unsigned int num) {
if (num == 0) {
return "0";
}

static char buf[21];
buf[20] = '\0';
int i = 20;

while (num > 0) {
buf[--i] = (num % 10) + '0';
num /= 10;
}

return &buf[i];
}

0 comments on commit e50401d

Please sign in to comment.