simplify pollfd deletion: by index

This commit is contained in:
root 2025-05-07 14:31:55 +00:00
parent f096cb00e7
commit d69183c0d1
2 changed files with 22 additions and 41 deletions

27
main.s
View file

@ -41,11 +41,7 @@ pollfds_len resb 8
SECTION .text
return:
ret
%include "server.s"
%include "pollfds_remove.s"
_start:
call make_server
@ -107,7 +103,7 @@ pollfds__scan__found__client:
mov edi, r14d
syscall
mov rdi, r14
mov rdi, r15
call pollfds__remove
pop rdi
@ -141,6 +137,27 @@ pollfds__append:
mov [pollfds_len], r10
ret
; rdi - pollfds array index to remove
pollfds__remove:
mov r11, [pollfds_len]
cmp rdi, r10
jge return ; XXX index out of bounds, do some other error?
cmp r11, 1
jle pollfds__clear
mov r10, [pollfds + (r11 - 1) * pollfd_size]
mov [pollfds + rdi * pollfd_size], r10
sub r11, 1
mov [pollfds_len], r11
ret
pollfds__clear:
mov r10, 0
mov [pollfds_len], r10
ret
return:
ret
exit:
mov rax, SYS_EXIT
mov rdi, 0

View file

@ -1,36 +0,0 @@
; rdi - fd
pollfds__remove:
mov rax, [pollfds_len]
mov rsi, 0
call pollfds__remove__loop
mov [pollfds_len], rax
ret
; Variables:
; rax - array length
; rdi - fd
; rsi - array index
; r11d - pollfds[rdi].fd
pollfds__remove__loop:
cmp rsi, rax
jge return ; fd was not found
mov r11d, [pollfds + rsi * pollfd_size]
add rsi, 1
cmp r11d, edi
jne pollfds__remove__loop
sub rsi, 1
; rax - array length
; rsi - array index to remove
pollfds__remove_index:
cmp rax, 1
jle pollfds__clear
mov r10d, [pollfds + (rax - 1) * pollfd_size]
mov [pollfds + rsi * pollfd_size], r10d
sub rax, 1
ret
; Returns new array length in rax
pollfds__clear:
mov rax, 0
ret