Wed, 29 Nov 2023 15:41:11 +0100
6 files changed,
137 insertions(+),
22 deletions(-)
M
common/memory.c
→
common/memory.c
@@ -1,10 +1,11 @@
#include "memory.h" +#include "string.h" #include "assert.h" struct memblock { struct memblock *next; - int size; + size_t size; }; volatile struct memblock volatile *malloc_first, *malloc_last, *malloc_next_free;@@ -26,6 +27,25 @@
return 0; } +static struct memblock* get_memblock_from_ptr(void* ptr) { + /* Just search through all blocks */ + struct memblock* next = malloc_first; + uint32_t target = (uint32_t)(ptr); + + while (next) { + uint32_t start = (uint32_t)(&next[1]); + uint32_t end = (uint32_t)(next->next); + + if (target >= start && target < end) { + return next; + } + + next = next->next; + } + + return NULL; +} + /*! * Allocates a block of \p size bytes of memory. (KERNEL MODE) * \param size Size in bytes of the requested block of memory.@@ -59,27 +79,29 @@
return pointer; } -/*! - * Frees a previously allocated block of memory. (KERNEL MODE) - * \param ptr Pointer to the memory block to be freed. - */ -void free(void* ptr) { - -} +void* realloc(void* ptr, size_t new_size) { + void* new_ptr = malloc(new_size); + struct memblock* block = get_memblock_from_ptr(ptr); + + if (block == NULL) { + /* corresponding block not found */ + return NULL; + } + if (block->size == 0) { + /* corresponding block was already freed */ + return NULL; + } + + memcpy(new_ptr, ptr, block->size); + free(ptr); -/*! - * Allocates a block of \p size bytes of memory. (USER MODE) - * \param size Size in bytes of the requested block of memory. - * \return Memory address to the new memory block - */ -void* os_user_malloc(size_t size) { - return (void*)0; + return new_ptr; } /*! - * Frees a previously allocated block of memory. (USER MODE) + * Frees a previously allocated block of memory. (KERNEL MODE) * \param ptr Pointer to the memory block to be freed. */ -void os_user_free(void* ptr) { +void free(void* ptr) { -}+}
M
common/memory.h
→
common/memory.h
@@ -21,9 +21,18 @@
/*! * Allocates a block of \p size bytes of memory. * \param size Size in bytes of the requested block of memory. - * \return Memory address to the new memory block + * \return Memory address to the new memory block, or NULL if failed */ void* malloc(size_t size); + +/*! + * Reallocates an already allocated block with new size. + * Contents are preserved. Pointer might change. + * Will fail if memory block at \p ptr has already been freed. + * \param ptr Pointer to already allocated memory block + * \param new_size Size of the new block + * \return Memory address to the new memory block, or NULL if failed*/ +void* realloc(void* ptr, size_t new_size); /*! * Frees a previously allocated block of memory.
M
libcedos/stdlib.h
→
libcedos/stdlib.h
@@ -26,6 +26,15 @@ */
void* malloc(size_t size); /*! + * Reallocates an already allocated block with new size. + * Contents are preserved. Pointer might change. + * Will fail if memory block at \p ptr has already been freed. + * \param ptr Pointer to already allocated memory block + * \param new_size Size of the new block + * \return Memory address to the new memory block, or NULL if failed*/ +void* realloc(void* ptr, size_t new_size); + +/*! * Frees a previously allocated block of memory. * \param ptr Pointer to the memory block to be freed. */
M
makefile
→
makefile
@@ -52,6 +52,11 @@ CCFLAGS := $(CCFLAGS) -O1
GLOBAL_BUILD := $(GLOBAL_BUILD)/release endif +ifndef VERBOSE +.SILENT: +> echo "Silent mode active." +endif + BUILD_OBJECTS := $(GLOBAL_BUILD)/obj BUILD_ARTIFACTS := $(GLOBAL_BUILD)/artifacts BUILD_LOGS := $(GLOBAL_BUILD)/logs@@ -71,6 +76,9 @@ DIRS := $(sort $(DIRS))
$(DIRS): > $(MKDIR) $@ +ifndef VERBOSE +> echo "MKDIR $@" +endif .PHONY: build build: $(BUILD_ARTIFACTS)/cedos.img@@ -78,6 +86,9 @@ $(OBJECTS): $(DIRS)
$(BUILD_OBJECTS)/%.s.o: %.s > $(AS) -o $@ $< +ifndef VERBOSE +> echo "AS $@" +endif # common OBJ_COMMON := $(filter $(BUILD_OBJECTS)/common/%,$(OBJECTS))@@ -91,12 +102,21 @@ OUT_BOOT := $(BUILD_ARTIFACTS)/boot.elf $(BUILD_ARTIFACTS)/boot.bin
$(BUILD_OBJECTS)/boot/%.c.o: boot/%.c > $(CC) -c -Iboot -Icommon $(CCFLAGS) -o $@ $< +ifndef VERBOSE +> echo "CC $@" +endif $(BUILD_ARTIFACTS)/boot.elf: $(OBJ_BOOT) $(OBJ_COMMON) > $(LD) $^ -T boot/link.txt -Map=$(BUILD_LOGS)/boot_mapfile.txt --oformat elf32-i386 -o $@ +ifndef VERBOSE +> echo "LD $@" +endif $(BUILD_ARTIFACTS)/boot.bin: $(OBJ_BOOT) $(OBJ_COMMON) > $(LD) $^ -T boot/link.txt -Map=$(BUILD_LOGS)/boot_mapfile.txt --oformat binary -o $@ +ifndef VERBOSE +> echo "LD $@" +endif .PHONY: boot boot: $(OUT_BOOT)@@ -108,12 +128,21 @@ OUT_KERNEL := $(BUILD_ARTIFACTS)/kernel.elf $(BUILD_ARTIFACTS)/kernel.bin
$(BUILD_OBJECTS)/kernel/%.c.o: kernel/%.c > $(CC) -c -Ikernel -Icommon $(CCFLAGS) -o $@ $< +ifndef VERBOSE +> echo "CC $@" +endif $(BUILD_ARTIFACTS)/kernel.elf: $(OBJ_KERNEL) $(OBJ_COMMON) > $(LD) $^ -T kernel/link.txt -Map=$(BUILD_LOGS)/kernel_mapfile.txt --oformat elf32-i386 -o $@ +ifndef VERBOSE +> echo "LD $@" +endif $(BUILD_ARTIFACTS)/kernel.bin: $(OBJ_KERNEL) $(OBJ_COMMON) > $(LD) $^ -T kernel/link.txt -Map=$(BUILD_LOGS)/kernel_mapfile.txt --oformat binary -o $@ +ifndef VERBOSE +> echo "LD $@" +endif .PHONY: kernel kernel: $(OUT_KERNEL)@@ -125,9 +154,15 @@ OUT_LIBCEDOS := $(BUILD_ARTIFACTS)/libcedos.a
$(BUILD_OBJECTS)/libcedos/%.c.o: libcedos/%.c > $(CC) -c -Ilibcedos -Icommon $(CCFLAGS) -o $@ $< +ifndef VERBOSE +> echo "CC $@" +endif $(BUILD_ARTIFACTS)/libcedos.a: $(OBJ_LIBCEDOS) $(OBJ_COMMON) > $(AR) rcs $@ $^ +ifndef VERBOSE +> echo "AR $@" +endif .PHONY: libcedos libcedos: $(OUT_LIBCEDOS)@@ -147,9 +182,15 @@ SHELL_LDFLAGS += -N
$(BUILD_OBJECTS)/shell/%.c.o: shell/%.c > $(CC) -c -Ishell -Ilibcedos -Icommon $(CCFLAGS) $(SHELL_CCFLAGS) -o $@ $< +ifndef VERBOSE +> echo "CC $@" +endif $(BUILD_ARTIFACTS)/%: $(BUILD_OBJECTS)/shell/%.c.o | $(BUILD_ARTIFACTS)/libcedos.a > $(LD) $^ $(LDFLAGS) $(SHELL_LDFLAGS) -o $@ +ifndef VERBOSE +> echo "LD $@" +endif .PHONY: shell shell: $(OUT_SHELL)@@ -167,6 +208,9 @@ > sudo cp ./img-contents/* $(BUILD_MOUNT) || echo "No img-contents folder; Skipping."
> du -csh $(BUILD_MOUNT)/* > sudo umount $(BUILD_MOUNT) > rm -r $(BUILD_MOUNT) +ifndef VERBOSE +> echo "MKFS $@" +endif $(BUILD_ARTIFACTS)/cedos.img: $(BUILD_ARTIFACTS)/fat.img | $(MODULES) > dd if=/dev/zero of=$@ count=904@@ -179,7 +223,9 @@ > dd if=$(BUILD_ARTIFACTS)/boot.bin of=$@ skip=1 seek=1 count=7 conv=notrunc
> python3 binimg.py -w 256 -i $(BUILD_ARTIFACTS)/cedos.img -o $(BUILD_ARTIFACTS)/cedos.png > parted $@ print list all # > $(LD) $(OBJECTS) -T link.txt -Map=$(BUILD_LOGS)/bin_mapfile.txt --oformat binary --nostdlib -o $@ - +ifndef VERBOSE +> echo "MKPART $@" +endif .PHONY: logs logs: $(BUILD_LOGS)/base.sym $(BUILD_LOGS)/objdump.txt
M
run.sh
→
run.sh
@@ -1,5 +1,5 @@
if [ -z "$QEMU_VNC" ]; then - qemu-system-i386 -drive index=0,if=floppy,format=raw,file=${1} -m 64 -monitor stdio -no-reboot -d int,cpu_reset,exec,in_asm -vga std 2> log/run_err.log + qemu-system-i386 -drive index=0,if=floppy,format=raw,file=${1} -m 64 -monitor stdio -no-reboot -d int,cpu_reset,exec,in_asm -vga std 2> build/release/logs/run_err.log else - qemu-system-i386 -drive index=0,if=floppy,format=raw,file=${1} -m 64 -monitor stdio -no-reboot -d int,cpu_reset,exec,in_asm -vga std -vnc :0 2> log/run_err.log + qemu-system-i386 -drive index=0,if=floppy,format=raw,file=${1} -m 64 -monitor stdio -no-reboot -d int,cpu_reset,exec,in_asm -vga std -vnc :0 2> build/release/logs/run_err.log fi
A
shell/mtest.c
@@ -0,0 +1,29 @@
+#include "stdio.h" +#include "stdlib.h" +#include "string.h" + +int main(char *args) { + printf("Malloc test\n"); + + void* a = malloc(1024); + printf("addr a: %p\n", a); + + void* b = malloc(1024); + printf("addr a: %p\n", b); + + printf("Copying test strings.\n"); + + strcpy(a, "This is test string a."); + strcpy(b, "Test string b is this."); + + printf("String in a: %s\n", a); + printf("String in b: %s\n", b); + + printf("Testing realloc.\n"); + + a = realloc(a, 512); + b = realloc(b, 2048); + + printf("Value of a at addr %p: %s\n", a, a); + printf("Value of b at addr %p: %s\n", b, b); +}