rdos/lisp/heap.asm
2019-03-23 20:13:05 +00:00

59 lines
1.1 KiB
NASM

verbose_malloc:
call malloc
push si ; both backup and input value for kprintf
mov si,.fmt
push di
call kprintf
pop di
pop si
ret
.fmt:
db "malloc %X", 0
; Allocates memory on the kernel heap
; Strategy: smallest fit
; in cx size in bytes
; dl node type
; out ds:di pointer
;
; Header:
; word ptr to next item (this implies its size)
; byte 00 = free, >0 used (refcount)
; byte high-level type
malloc:
push ax
push bx
mov di,heap
add cx, 0x04 ; header length
jmp .check
.next:
add di,[di]
.check:
mov ax,[di]
test ax,0xFFFF
jz .append ; we are on last entry
cmp byte [di+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, not implemented yet
.reuse: ; exact length
mov byte [di+2],0xFF
jmp .end
.append: ; append to last item
mov word [di], cx
mov byte [di+2], 0x00
mov byte [di+3], dl
push di
add word di, [di]
mov byte [di],0x0000
pop di
.end:
pop bx
pop ax
ret
; Deallocates memory on the kernel heap
free: