123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- ; vim:ft=fasm:
- ; TODO: add arena allocator
- ; TODO: add general purpose allocator
- interface Allocator
- ; u8* alloc(usize)
- alloc size
- ; u8* realloc(u8*, usize)
- realloc ptr, new_size
- ; void realloc(u8*)
- free ptr
- end interface
- struct FixedBufferAllocator.data
- memory dq ?
- size dq ?
- capacity dq ?
- alignment db 8
- ends
- macro FixedBufferAllocator.data.new _data*, _memory*, _capacity*, _size:0, _alignment:8
- mov qword [_data + FixedBufferAllocator.data.memory], _memory
- mov qword [_data + FixedBufferAllocator.data.size], _size
- mov qword [_data + FixedBufferAllocator.data.capacity], _capacity
- mov byte [_data + FixedBufferAllocator.data.alignment], _alignment
- end macro
- proc FixedBufferAllocator.reset
- using_regs rax
- interface.self_data rax
- mov [rax + FixedBufferAllocator.data.size], 0
- end using_regs
- end proc
- proc FixedBufferAllocator.alloc
- .self_data = r15
- .new_size = r14
- .old_size = r13
- .size = rdi
- ;.alignment = 8
- using_regs .self_data, .new_size, .old_size
- interface.self_data rax, .self_data
- .check_size:
- mov .old_size, [.self_data + FixedBufferAllocator.data.size]
- lea .new_size, [.old_size + .size]
- movzx rsi, byte [.self_data + FixedBufferAllocator.data.alignment]
- align_to .new_size, rsi
- mov .new_size, rax
- xor rax, rax
- cmp [.self_data + FixedBufferAllocator.data.capacity], .new_size
- jl .capcity_exceeded
- .capacity_ok:
- mov [.self_data + FixedBufferAllocator.data.size], .new_size
- mov rax, [.self_data + FixedBufferAllocator.data.memory]
- lea rax, [rax + .old_size]
- .capcity_exceeded:
- end using_regs
- end proc
- proc Allocator.realloc_default
- xor rax, rax
- end proc
- proc Allocator.free_default
- xor rax, rax
- end proc
|