Skip to content

Commit

Permalink
Added kfree function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryand1234 committed May 17, 2024
1 parent 5957f07 commit 96995a1
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion kernel/arch/i386/linker.ld
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ SECTIONS
}
.page_tables : {
__page_tables_start = .;
. += 4096 * 4;
. += 4096 * 1;
}

/* The compiler may produce other sections, put them in the proper place in
Expand Down
2 changes: 1 addition & 1 deletion kernel/arch/i386/tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "vga.h"

static const size_t VGA_WIDTH = 80;
static const size_t VGA_HEIGHT = 25;
static const size_t VGA_HEIGHT = 100;
static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000;

static size_t terminal_row;
Expand Down
5 changes: 5 additions & 0 deletions kernel/kernel/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ void kernel_main(void) {

char* test = (char*) kmalloc(20);
printf("Memory Address %d\n", test);
char* test1 = (char*) kmalloc(50);
printf("Memory Address for test1: %d\n", test1);
kfree(test);
char* test2= (char*) kmalloc(20);
printf("Memory Address for test2: %d\n", test2);
divide_by_zero();
printf("DONE");
}
Expand Down
27 changes: 20 additions & 7 deletions kernel/mmu/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
#include<stdint.h>
void kfree(void *v_addr)
{
printf("KFREEEEEEEE\n");
// printf("KFREEEEEEEE\n");
if(v_addr == (void*)0)
return ;

struct kmalloc_header *chunk, *other;

chunk = (struct kmalloc_header*) ((uint32_t)v_addr - sizeof(struct kmalloc_header));

//printf("FREE CHUNK: %d, %d, %d\n", chunk, chunk->size, chunk->used);

chunk->used = 0;

kmalloc_used -= chunk->size;
while((other = (struct kmalloc_header*)((char*)chunk + chunk->size)) && other < (struct kmalloc_header*) kern_heap && other->used == 0)
{
chunk->size = chunk->size + other->size;
}

}
void *ksbrk(uint16_t n)
{
printf("BREakkkkkk\n");

struct kmalloc_header* chunk;
char *p_addr;
uint32_t i;
printf("KSBRK: N: %d\n", n);

if((kern_heap + (n*PAGESIZE)) > (char*) KERN_HEAP_LIM) {
printf("PANIC ksbrk(): no memory left\n");
Expand Down Expand Up @@ -62,10 +75,10 @@ void *ksbrk(uint16_t n)
return 0;
}
chunk = (struct kmalloc_header*) ((char*)chunk + chunk->size);
if(chunk == (struct kmalloc_header*) kern_heap)
// printf("CHUNK:%d, %d, %d, HEAP: %d\n", chunk->size, chunk->used, chunk, (struct kmalloc_header*)kern_heap);
if(chunk == (struct kmalloc_header*) kern_heap || ((struct kmalloc_header*) kern_heap - chunk) < realsize)
{
chunk = (ksbrk((realsize/PAGESIZE)+1));
if((int)chunk < 0)
if((int)(ksbrk((realsize/PAGESIZE)+1)) < 0)
{
printf("No memory left %d, heap: %d", chunk, kern_heap);
asm("hlt");
Expand Down

0 comments on commit 96995a1

Please sign in to comment.