123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- ; vim:ft=fasm:
- ;include 'utils.inc'
- ;include 'align.inc'
- ;include 'struct.inc'
- ;include 'interface.inc'
- ;if ~ defined Writer
- interface Writer
- write buf, size
- end interface
- struct _Buffer
- data dq ?
- size dq ?
- capacity dq ?
- ends
- struct BufferedWriter.data
- buf _Buffer
- writer dq ?
- ends
- BufferedWriter.buffer_size = (byte * 0x1000 * 0x80)
- proc BufferedWriter.write
- .self = r15
- .self.data = r14
- .new_size = r13
- .old_size = r12
- .buf = rdi
- .len = rsi
- using_regs .self, .self.data, .new_size, .old_size
- mov .self, rax
- interface.self_data .self, .self.data
- .check_size:
- mov .old_size, [.self.data + BufferedWriter.data.buf.size]
- lea .new_size, [.old_size + .len]
- cmp [.self.data + BufferedWriter.data.buf.capacity], .new_size
- jge .capcity_ok
- .capcity_exceeded:
- mov rax, .self
- using_regs .len, .buf
- call BufferedWriter.flush ; TODO: check fail
- end using_regs
- cmp [.self.data + BufferedWriter.data.buf.capacity], .len
- jge .check_size
- .write_too_big:
- using_regs .len, .buf
- Writer.write [.self.data + BufferedWriter.data.writer], .buf, .len
- end using_regs
- jmp .end_reg
- .capcity_ok:
- mov rdx, .len
- .len = rdx
- mov rsi, .buf
- .buf = rsi
- mov rax, [.self.data + BufferedWriter.data.buf.data]
- lea rdi, [rax + .old_size]
- memcpy rdi, .buf, .len
- mov [.self.data + BufferedWriter.data.buf.size], .new_size
-
- .end_reg:
- end using_regs
- end proc
- proc BufferedWriter.flush
- .self_data = r15
- using_regs .self_data
- interface.self_data rax, .self_data
- Writer.write [.self_data + BufferedWriter.data.writer], [.self_data + BufferedWriter.data.buf.data], [.self_data + BufferedWriter.data.buf.size]
- mov qword [.self_data + BufferedWriter.data.buf.size], 0
- end using_regs
- end proc
- proc FDWriter.write
- .buf = rsi
- .len = rdx
- mov .len, rsi
- mov .buf, rdi
- interface.self_data rax
- write dword [rax], .buf, .len
- end proc
- ;include 'stdlib.inc'
- ;end if
|