added bad malloc
This commit is contained in:
parent
c6527da081
commit
e5c428e6cf
2
Makefile
2
Makefile
|
@ -3,7 +3,7 @@ GCCPATH = /opt/gcc-arm/bin
|
||||||
CC = $(GCCPATH)/aarch64-none-elf-gcc
|
CC = $(GCCPATH)/aarch64-none-elf-gcc
|
||||||
LD = $(GCCPATH)/aarch64-none-elf-ld
|
LD = $(GCCPATH)/aarch64-none-elf-ld
|
||||||
OBJCOPY = $(GCCPATH)/aarch64-none-elf-objcopy
|
OBJCOPY = $(GCCPATH)/aarch64-none-elf-objcopy
|
||||||
GCCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles
|
GCCFLAGS = -Wall -O2 -ffreestanding -fno-builtin -nostdinc -nostdlib -nostartfiles
|
||||||
|
|
||||||
OUTDIR = ./out/bin
|
OUTDIR = ./out/bin
|
||||||
|
|
||||||
|
|
8
include/memory.h
Normal file
8
include/memory.h
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define NULL ((void*)0)
|
||||||
|
|
||||||
|
void heap_init();
|
||||||
|
|
||||||
|
void* malloc();
|
||||||
|
void free(void* ptr);
|
|
@ -1,5 +1,6 @@
|
||||||
#include "convert.h"
|
#include "convert.h"
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
int stoi(const char* string)
|
int stoi(const char* string)
|
||||||
{
|
{
|
||||||
|
@ -29,7 +30,9 @@ char* ultoa(unsigned long number, char* string, int base)
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
volatile char* temp = (volatile char*)0x40000000;
|
char* buffer = (char*)malloc();
|
||||||
|
char* temp = buffer;
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
if(number == 0)
|
if(number == 0)
|
||||||
{
|
{
|
||||||
|
@ -39,7 +42,12 @@ char* ultoa(unsigned long number, char* string, int base)
|
||||||
{
|
{
|
||||||
for(; number > 0; number /= base)
|
for(; number > 0; number /= base)
|
||||||
{
|
{
|
||||||
temp[i++] = '0' + (number % base);
|
uint8_t digit = number % base;
|
||||||
|
|
||||||
|
if(digit < 10)
|
||||||
|
temp[i++] = '0' + digit;
|
||||||
|
else
|
||||||
|
temp[i++] = 'A' + digit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,6 +59,7 @@ char* ultoa(unsigned long number, char* string, int base)
|
||||||
string[--i] = *temp++;
|
string[--i] = *temp++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "convert.h"
|
#include "convert.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
void print_clippy();
|
void print_clippy();
|
||||||
char* get_user_input(char* buffer);
|
char* get_user_input(char* buffer);
|
||||||
|
@ -12,7 +13,8 @@ void main()
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
|
|
||||||
uart_init();
|
uart_init();
|
||||||
|
heap_init();
|
||||||
|
|
||||||
print_clippy();
|
print_clippy();
|
||||||
uart_puts("Boot successful! \n");
|
uart_puts("Boot successful! \n");
|
||||||
uart_puts("Started execution at 0x");
|
uart_puts("Started execution at 0x");
|
||||||
|
|
70
src/memory.c
Normal file
70
src/memory.c
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
#include "stdint.h"
|
||||||
|
#include "io.h"
|
||||||
|
#include "convert.h"
|
||||||
|
|
||||||
|
struct FreeBlock
|
||||||
|
{
|
||||||
|
struct FreeBlock* next;
|
||||||
|
};
|
||||||
|
|
||||||
|
#define HEAP_BASE ((void*)0x40000000)
|
||||||
|
#define BLOCK_SIZE 4096
|
||||||
|
#define MAGIC_NUMBER 0xDEADBEEF
|
||||||
|
|
||||||
|
static struct FreeBlock* head = (struct FreeBlock*)HEAP_BASE;
|
||||||
|
|
||||||
|
void heap_init()
|
||||||
|
{
|
||||||
|
head->next = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* malloc()
|
||||||
|
{
|
||||||
|
void* block = (void*)head;
|
||||||
|
|
||||||
|
if(head->next == NULL)
|
||||||
|
{
|
||||||
|
head += BLOCK_SIZE;
|
||||||
|
head->next = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
head = head->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
*(uint32_t*)block = MAGIC_NUMBER;
|
||||||
|
return block + sizeof(uint32_t);
|
||||||
|
}
|
||||||
|
|
||||||
|
void free(void* ptr)
|
||||||
|
{
|
||||||
|
void* blockStart = ptr - sizeof(uint32_t);
|
||||||
|
if(*(uint32_t*)blockStart != MAGIC_NUMBER)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if((struct FreeBlock*)blockStart < head)
|
||||||
|
{
|
||||||
|
struct FreeBlock* nextBlock = head->next;
|
||||||
|
head = blockStart;
|
||||||
|
head->next = nextBlock;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FreeBlock* block = head;
|
||||||
|
|
||||||
|
while(block)
|
||||||
|
{
|
||||||
|
if(block < (struct FreeBlock*)blockStart && (struct FreeBlock*)blockStart < block->next)
|
||||||
|
{
|
||||||
|
struct FreeBlock* newBlock = (struct FreeBlock*)blockStart;
|
||||||
|
newBlock->next = block->next;
|
||||||
|
block->next = newBlock;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
block = block->next;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue