From 8d541ce498be46142d09a41ceea4d790301f1a98 Mon Sep 17 00:00:00 2001 From: Ain <41307858+nero@users.noreply.github.com> Date: Sat, 16 Mar 2019 10:46:01 +0000 Subject: [PATCH] PoC for heap --- heap.asm | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.asm | 11 ++++++++--- 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 heap.asm diff --git a/heap.asm b/heap.asm new file mode 100644 index 0000000..9035b6a --- /dev/null +++ b/heap.asm @@ -0,0 +1,55 @@ +verbose_malloc: + call malloc + push si ; both backup and input value for kprintf + mov si,.fmt + call kprintf + pop si + ret +.fmt: + db "malloc %X", 0 + +; Allocates memory on the kernel heap +; Strategy: smallest fit +; in cx size in bytes +; out si pointer +; +; Header: +; word ptr to next item (this implies its size) +; byte 00 = free, FF = in use +malloc: + push ax + mov si,heap + jmp .check +.next: + mov si,[si] +.check: + mov ax,[si] + test ax,0xFFFF + jz .append ; we are on last entry + cmp byte [si+2],0x00 + jne .next ; entry is in use + sub ax,cx + je .reuse ; entry has exact length + jc .next ; entry too short + jmp .next ; we'd have to split +.reuse: ; exact length + mov byte [si+2],0xFF + add si, 0x03 + pop ax + ret +.append: ; append to last item + mov ax, si + add ax, 3 + add ax, cx + mov word [si], ax + mov byte [si+2], 0xFF + push si + mov word si,[si] + mov byte [si],0x0000 + pop si + add si, 0x03 + pop ax + ret + +; Deallocates memory on the kernel heap +free: diff --git a/main.asm b/main.asm index 7c865f6..4af2c0b 100644 --- a/main.asm +++ b/main.asm @@ -48,12 +48,17 @@ main: call ivt_set - int 0x2E - cmp ax,0x55AA - int 0x2E + mov cx, 0x0010 + call verbose_malloc + call verbose_malloc + call verbose_malloc ret %include "intr.asm" %include "debug.asm" %include "kprintf.asm" +%include "heap.asm" + +heap: + dw 0