update gc to use alloc. Still buggy
This commit is contained in:
@@ -1,13 +1,12 @@
|
|||||||
global gc_init, gc_alloc
|
global gc_init, gc_alloc
|
||||||
|
extern alloc, free
|
||||||
|
|
||||||
section .bss
|
section .bss
|
||||||
alloc_list: resq 1 ; head of alloc list
|
alloc_list: resq 1 ; head of alloc list
|
||||||
stack_start: resq 1 ; start of stack
|
stack_start: resq 1 ; start of stack
|
||||||
|
|
||||||
section .data
|
section .data
|
||||||
gc_threshold_b: dq 4096 ; default of 4096 bytes, this will scale when gc_collect is ran
|
gc_threshold_c: dq 1024 ; default of 1024 allocations
|
||||||
gc_threshold_c: dq 1024 ; default of 1024 allocations
|
|
||||||
total_alloc_b: dq 0 ; counts the allocated bytes
|
|
||||||
total_alloc_c: dq 0 ; count the amount of allocations
|
total_alloc_c: dq 0 ; count the amount of allocations
|
||||||
|
|
||||||
section .text
|
section .text
|
||||||
@@ -17,22 +16,16 @@ gc_init:
|
|||||||
|
|
||||||
gc_alloc:
|
gc_alloc:
|
||||||
add rdi, 24 ; add space for metadata
|
add rdi, 24 ; add space for metadata
|
||||||
mov rdx, [total_alloc_b] ; load total allocations in bytes
|
|
||||||
cmp rdx, [gc_threshold_b] ; has total exceeded threshold?
|
|
||||||
jae .collect ; yes? run gc
|
|
||||||
mov rdx, [total_alloc_c] ; load total allocation count
|
mov rdx, [total_alloc_c] ; load total allocation count
|
||||||
cmp rdx, [gc_threshold_c] ; has count exceeded threshold?
|
cmp rdx, [gc_threshold_c] ; has count exceeded threshold?
|
||||||
jae .collect ; yes? run gc
|
jb .skip_collect ; yes? run gc
|
||||||
jmp .collect_end
|
|
||||||
.collect:
|
|
||||||
push rdi
|
push rdi
|
||||||
call gc_collect
|
call gc_collect
|
||||||
pop rdi
|
pop rdi
|
||||||
.collect_end:
|
.skip_collect:
|
||||||
add [total_alloc_b], rdi ; update total allocated bytes
|
|
||||||
inc qword [total_alloc_c] ; update total allocation count
|
inc qword [total_alloc_c] ; update total allocation count
|
||||||
push rdi
|
push rdi
|
||||||
call sys_mmap ; allocate size + metadata
|
call alloc ; allocate size + metadata
|
||||||
pop rdi
|
pop rdi
|
||||||
mov byte [rax], 0 ; set mark to 0
|
mov byte [rax], 0 ; set mark to 0
|
||||||
mov qword [rax + 8], rdi ; set total size of object (including metadata)
|
mov qword [rax + 8], rdi ; set total size of object (including metadata)
|
||||||
@@ -45,12 +38,7 @@ gc_alloc:
|
|||||||
gc_collect:
|
gc_collect:
|
||||||
call gc_mark_stack
|
call gc_mark_stack
|
||||||
call gc_sweep
|
call gc_sweep
|
||||||
mov qword [total_alloc_c], 0 ; reset allocation count
|
mov qword [total_alloc_c], 0 ; reset allocation count
|
||||||
mov rdi, [total_alloc_b] ; since we just swept, all the memory is in use
|
|
||||||
shl rdi, 1 ; double the currently used memory
|
|
||||||
mov rsi, 4096
|
|
||||||
call max ; get the largest of total_alloc_b * 2 and 4096
|
|
||||||
mov qword [gc_threshold_b], rax ; update threshold to new value
|
|
||||||
ret
|
ret
|
||||||
|
|
||||||
gc_mark_stack:
|
gc_mark_stack:
|
||||||
@@ -58,10 +46,10 @@ gc_mark_stack:
|
|||||||
mov r9, [stack_start] ; load start of stack
|
mov r9, [stack_start] ; load start of stack
|
||||||
.loop:
|
.loop:
|
||||||
cmp r8, r9 ; have we reached end of stack?
|
cmp r8, r9 ; have we reached end of stack?
|
||||||
ja .done ; yes? return
|
jae .done ; yes? return
|
||||||
mov rdi, [r8] ; no? load the value
|
mov rdi, [r8] ; no? load the value
|
||||||
call gc_mark ; this might be an allocation, check
|
call gc_mark ; this might be an allocation, check
|
||||||
add r8, 8 ; next item in stack
|
lea r8, [r8 + 8] ; next item in stack
|
||||||
jmp .loop
|
jmp .loop
|
||||||
.done:
|
.done:
|
||||||
ret
|
ret
|
||||||
@@ -100,68 +88,29 @@ gc_sweep:
|
|||||||
mov rdi, [alloc_list]
|
mov rdi, [alloc_list]
|
||||||
xor rsi, rsi
|
xor rsi, rsi
|
||||||
.loop:
|
.loop:
|
||||||
test rdi, rdi ; reached end of list?
|
test rdi, rdi ; reached end of list?
|
||||||
jz .done ; yes? return
|
jz .done ; yes? return
|
||||||
mov al, [rdi]
|
mov al, [rdi]
|
||||||
test al, al ; is object marked?
|
test al, al ; is object marked?
|
||||||
jz .free ; no? free it
|
jz .free ; no? free it
|
||||||
mov byte [rdi], 0 ; yes? clear mark for next marking
|
mov byte [rdi], 0 ; yes? clear mark for next marking
|
||||||
mov rsi, rdi
|
mov rsi, rdi
|
||||||
mov rdi, [rdi + 16] ; load the next object in the list
|
mov rdi, [rdi + 16] ; load the next object in the list
|
||||||
jmp .loop ; repeat
|
jmp .loop ; repeat
|
||||||
.free:
|
.free:
|
||||||
mov rdx, [rdi + 16] ; save address of next object in list
|
mov rdx, [rdi + 16] ; save address of next object in list
|
||||||
test rsi, rsi
|
test rsi, rsi
|
||||||
jz .remove_head
|
jz .remove_head
|
||||||
mov [rsi + 16], rdx ; unlink the current node by setting the previous node's next to the next node's address
|
mov [rsi + 16], rdx ; unlink the current node by setting the previous node's next to the next node's address
|
||||||
jmp .free_memory
|
jmp .free_memory
|
||||||
.remove_head:
|
.remove_head:
|
||||||
mov [alloc_list], rdx ; update head node to be the next node
|
mov [alloc_list], rdx ; update head node to be the next node
|
||||||
.free_memory:
|
.free_memory:
|
||||||
push rsi ; save previous node since it will also be the previous node for the next item
|
push rsi ; save previous node since it will also be the previous node for the next item
|
||||||
push rdx ; save next node
|
push rdx ; save next node
|
||||||
mov rsi, [rdi + 8] ; get length of the object
|
call free ; free the memory
|
||||||
sub [total_alloc_b], rsi ; remove this allocation from total allocations
|
pop rdi ; input for next iteration
|
||||||
call sys_munmap ; free the memory
|
pop rsi ; prev node for next iteration
|
||||||
pop rdi ; input for next iteration
|
|
||||||
pop rsi ; prev node for next iteration
|
|
||||||
jmp .loop
|
jmp .loop
|
||||||
.done:
|
.done:
|
||||||
ret
|
ret
|
||||||
|
|
||||||
sys_mmap:
|
|
||||||
mov rax, 9
|
|
||||||
mov rsi, rdi
|
|
||||||
mov rdi, 0
|
|
||||||
mov rdx, 3
|
|
||||||
mov r10, 34
|
|
||||||
mov r8, -1
|
|
||||||
mov r9, 0
|
|
||||||
syscall
|
|
||||||
cmp rax, -1
|
|
||||||
je .error
|
|
||||||
ret
|
|
||||||
.error:
|
|
||||||
mov rax, 60
|
|
||||||
mov rdi, 1
|
|
||||||
syscall
|
|
||||||
|
|
||||||
sys_munmap:
|
|
||||||
mov rax, 11
|
|
||||||
syscall
|
|
||||||
cmp rax, -1
|
|
||||||
je .error
|
|
||||||
ret
|
|
||||||
.error:
|
|
||||||
mov rax, 60
|
|
||||||
mov rdi, 1
|
|
||||||
syscall
|
|
||||||
|
|
||||||
max:
|
|
||||||
cmp rdi, rsi
|
|
||||||
jae .left
|
|
||||||
mov rax, rsi
|
|
||||||
ret
|
|
||||||
.left:
|
|
||||||
mov rax, rdi
|
|
||||||
ret
|
|
||||||
Reference in New Issue
Block a user