Implement block-based heap
This commit is contained in:
parent
6f7815bbe2
commit
0414af640a
@ -1,12 +1,14 @@
|
|||||||
heap_size:
|
heap_size:
|
||||||
dw 0x0100
|
; 1 step here means 1 byte heap alloc table + 16 byte heap data
|
||||||
|
; 0x0400 = 1kB table, 16kb data
|
||||||
|
dw 0x0400
|
||||||
|
|
||||||
heap_init:
|
heap_init:
|
||||||
push ax ; byte value (0)
|
push ax ; byte value (0)
|
||||||
push cx ; loop counter
|
push cx ; loop counter
|
||||||
push di ; target ptr
|
push di ; target ptr
|
||||||
push es
|
push es
|
||||||
mov ax, ds
|
mov ax, cs
|
||||||
mov es, ax
|
mov es, ax
|
||||||
xor ax, ax
|
xor ax, ax
|
||||||
mov cx, [heap_size]
|
mov cx, [heap_size]
|
||||||
@ -21,12 +23,62 @@ heap_init:
|
|||||||
; in ax number of bytes to alloc
|
; in ax number of bytes to alloc
|
||||||
; out ds:di
|
; out ds:di
|
||||||
malloc:
|
malloc:
|
||||||
push dx ; length of block index
|
push cx ; number of blocks to allocate
|
||||||
or ax,0x0F
|
push bp ; base offset for block table
|
||||||
inc ax
|
push es
|
||||||
mov dx, 0x100
|
|
||||||
mov bp, heap
|
|
||||||
|
|
||||||
int 0x2E
|
; transfer 12 MSB from ax to cx
|
||||||
pop dx
|
add ax, 0x000F
|
||||||
|
mov cl, 0x04
|
||||||
|
shr ax, cl
|
||||||
|
mov cx, ax
|
||||||
|
|
||||||
|
mov bp, heap
|
||||||
|
mov di, 0x0000
|
||||||
|
mov ax, ds
|
||||||
|
mov es, ax
|
||||||
|
xor ah, ah ; we use ax for reading from al once
|
||||||
|
jmp .firstblock
|
||||||
|
|
||||||
|
.testlength:
|
||||||
|
push cx
|
||||||
|
push di
|
||||||
|
add di, bp ; scasb doesn't add bp on its own
|
||||||
|
repe scasb
|
||||||
|
pop di
|
||||||
|
pop cx
|
||||||
|
je .found
|
||||||
|
; find next free item
|
||||||
|
mov al, 0x01
|
||||||
|
.nextblock:
|
||||||
|
add di, ax
|
||||||
|
.firstblock:
|
||||||
|
mov al, [bp+di]
|
||||||
|
cmp al, 0x00
|
||||||
|
jne .nextblock
|
||||||
|
cmp cx, 0x0001 ; fast case: 1 block allocation, skip test if length fits
|
||||||
|
jne .testlength
|
||||||
|
.found:
|
||||||
|
mov [bp+di], cl
|
||||||
|
mov cl, 0x04
|
||||||
|
shl di, cl
|
||||||
|
add di, bp
|
||||||
|
add di, [heap_size]
|
||||||
|
pop es
|
||||||
|
pop bp
|
||||||
|
pop cx
|
||||||
|
ret
|
||||||
|
|
||||||
|
; in ds:di begin of data block to free (di trashed)
|
||||||
|
free:
|
||||||
|
push cx
|
||||||
|
push bp
|
||||||
|
mov bp, heap
|
||||||
|
sub di, [heap_size]
|
||||||
|
sub di, bp
|
||||||
|
mov cl, 0x04
|
||||||
|
shr di, cl
|
||||||
|
mov byte [bp+di], 0x00
|
||||||
|
pop bp
|
||||||
|
pop cx
|
||||||
ret
|
ret
|
||||||
|
Loading…
Reference in New Issue
Block a user