rdos/heap.asm

55 lines
992 B
NASM
Raw Normal View History

2019-03-16 11:46:01 +01:00
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)
2019-03-16 12:08:11 +01:00
; byte 00 = free, >0 used (refcount)
2019-03-16 11:46:01 +01:00
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
2019-03-16 12:08:11 +01:00
jmp .next ; we'd have to split, not implemented yet
2019-03-16 11:46:01 +01:00
.reuse: ; exact length
mov byte [si+2],0xFF
2019-03-16 12:08:11 +01:00
jmp .end
2019-03-16 11:46:01 +01:00
.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
2019-03-16 12:08:11 +01:00
.end:
2019-03-16 11:46:01 +01:00
add si, 0x03
pop ax
ret
; Deallocates memory on the kernel heap
free: