allocator.inc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. ; vim:ft=fasm:
  2. ; TODO: add arena allocator
  3. ; TODO: add general purpose allocator
  4. interface Allocator
  5. ; u8* alloc(usize)
  6. alloc size
  7. ; u8* realloc(u8*, usize)
  8. realloc ptr, new_size
  9. ; void realloc(u8*)
  10. free ptr
  11. end interface
  12. struct FixedBufferAllocator.data
  13. memory dq ?
  14. size dq ?
  15. capacity dq ?
  16. alignment db 8
  17. ends
  18. macro FixedBufferAllocator.data.new _data*, _memory*, _capacity*, _size:0, _alignment:8
  19. mov qword [_data + FixedBufferAllocator.data.memory], _memory
  20. mov qword [_data + FixedBufferAllocator.data.size], _size
  21. mov qword [_data + FixedBufferAllocator.data.capacity], _capacity
  22. mov byte [_data + FixedBufferAllocator.data.alignment], _alignment
  23. end macro
  24. proc FixedBufferAllocator.reset
  25. using_regs rax
  26. interface.self_data rax
  27. mov [rax + FixedBufferAllocator.data.size], 0
  28. end using_regs
  29. end proc
  30. proc FixedBufferAllocator.alloc
  31. .self_data = r15
  32. .new_size = r14
  33. .old_size = r13
  34. .size = rdi
  35. ;.alignment = 8
  36. using_regs .self_data, .new_size, .old_size
  37. interface.self_data rax, .self_data
  38. .check_size:
  39. mov .old_size, [.self_data + FixedBufferAllocator.data.size]
  40. lea .new_size, [.old_size + .size]
  41. movzx rsi, byte [.self_data + FixedBufferAllocator.data.alignment]
  42. align_to .new_size, rsi
  43. mov .new_size, rax
  44. xor rax, rax
  45. cmp [.self_data + FixedBufferAllocator.data.capacity], .new_size
  46. jl .capcity_exceeded
  47. .capacity_ok:
  48. mov [.self_data + FixedBufferAllocator.data.size], .new_size
  49. mov rax, [.self_data + FixedBufferAllocator.data.memory]
  50. lea rax, [rax + .old_size]
  51. .capcity_exceeded:
  52. end using_regs
  53. end proc
  54. proc Allocator.realloc_default
  55. xor rax, rax
  56. end proc
  57. proc Allocator.free_default
  58. xor rax, rax
  59. end proc