PoC for heap

This commit is contained in:
Nero 2019-03-16 10:46:01 +00:00
parent ffba354a4e
commit 8d541ce498
2 changed files with 63 additions and 3 deletions

55
heap.asm Normal file
View File

@ -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:

View File

@ -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