PoC for heap
This commit is contained in:
parent
ffba354a4e
commit
8d541ce498
55
heap.asm
Normal file
55
heap.asm
Normal 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:
|
11
main.asm
11
main.asm
@ -48,12 +48,17 @@ main:
|
|||||||
|
|
||||||
call ivt_set
|
call ivt_set
|
||||||
|
|
||||||
int 0x2E
|
mov cx, 0x0010
|
||||||
cmp ax,0x55AA
|
call verbose_malloc
|
||||||
int 0x2E
|
call verbose_malloc
|
||||||
|
call verbose_malloc
|
||||||
|
|
||||||
ret
|
ret
|
||||||
|
|
||||||
%include "intr.asm"
|
%include "intr.asm"
|
||||||
%include "debug.asm"
|
%include "debug.asm"
|
||||||
%include "kprintf.asm"
|
%include "kprintf.asm"
|
||||||
|
%include "heap.asm"
|
||||||
|
|
||||||
|
heap:
|
||||||
|
dw 0
|
||||||
|
Loading…
Reference in New Issue
Block a user