56 lines
977 B
NASM
56 lines
977 B
NASM
|
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:
|