diff --git a/Makefile b/Makefile index ef6f291..083535e 100644 --- a/Makefile +++ b/Makefile @@ -33,10 +33,10 @@ CFLAGS += -DTEXT_IO_ADDR=0xff0000 -DTEXT_IO_BUFLEN=64 CFLAGS += -march=$(ARCH) # dependencies that need to be built: -_DEPS = ecall.c csr.c sched.c io.o +_DEPS = ecall.c csr.c sched.c io.c malloc.c # dependencies as object files: -_OBJ = ecall.o sched.o boot.o csr.o io.o +_OBJ = ecall.o sched.o boot.o csr.o io.o malloc.o DEPS = $(patsubst %,$(KLIBDIR)/%,$(_DEPS)) diff --git a/kinclude/malloc.c b/kinclude/malloc.c new file mode 100644 index 0000000..97a0342 --- /dev/null +++ b/kinclude/malloc.c @@ -0,0 +1,33 @@ +#include "malloc.h" +#include "ecall.h" + +malloc_info global_malloc_info = { 0 }; +void* allocate_memory_end; + + +void malloc_init(malloc_info* given_info) +{ + global_malloc_info = *given_info; + allocate_memory_end = given_info->allocate_memory_end; +} + +void* malloc(size_t size) +{ + return (void*) ENOMEM; +} + +int free(void* ptr) +{ + return EINVAL; +} + +// allocate stack and return a pointer to the *end* of the allocated region +void* malloc_stack(size_t size) +{ + void* new_alloc_end = (void*) (((int) allocate_memory_end) - size); + if (new_alloc_end < global_malloc_info.allocate_memory_start) + return (void*) ENOMEM; + void* stack_top = allocate_memory_end; + allocate_memory_end = new_alloc_end; + return stack_top; +} \ No newline at end of file diff --git a/kinclude/malloc.h b/kinclude/malloc.h new file mode 100644 index 0000000..687efc4 --- /dev/null +++ b/kinclude/malloc.h @@ -0,0 +1,18 @@ +#ifndef H_MALLOC +#define H_MALLOC + +typedef unsigned int size_t; + +typedef struct malloc_info { + void* allocate_memory_end; + void* allocate_memory_start; +} malloc_info; + +void* malloc(size_t size); +// int free(void* ptr); + +void* malloc_stack(size_t size); + +void malloc_init(malloc_info* info); + +#endif \ No newline at end of file