Implement Bytecode Executor for Kym #19
|
@ -24,11 +24,7 @@ pub const Allocator = struct {
|
|||
pub fn bind(comptime State: type, state: *State, comptime actions: Actions(State)) Allocator {
|
||||
const is_zero_aligned = @alignOf(State) == 0;
|
||||
|
||||
return .{
|
||||
.context = if (is_zero_aligned) state else @ptrCast(state),
|
||||
|
||||
.actions = &.{
|
||||
.deallocate = struct {
|
||||
const ErasedActions = struct {
|
||||
fn deallocate(context: *anyopaque, allocation: []Byte) void {
|
||||
if (is_zero_aligned) {
|
||||
return actions.deallocator(@ptrCast(context), allocation);
|
||||
|
@ -36,9 +32,7 @@ pub const Allocator = struct {
|
|||
|
||||
return actions.deallocate(@ptrCast(@alignCast(context)), allocation);
|
||||
}
|
||||
}.deallocate,
|
||||
|
||||
.reallocate = struct {
|
||||
fn reallocate(context: *anyopaque, return_address: usize, existing_allocation: ?[]Byte, size: usize) AllocationError![]Byte {
|
||||
if (is_zero_aligned) {
|
||||
return actions.reallocator(@ptrCast(context), return_address, existing_allocation, size);
|
||||
|
@ -46,7 +40,14 @@ pub const Allocator = struct {
|
|||
|
||||
return actions.reallocate(@ptrCast(@alignCast(context)), return_address, existing_allocation, size);
|
||||
}
|
||||
}.reallocate,
|
||||
};
|
||||
|
||||
return .{
|
||||
.context = if (is_zero_aligned) state else @ptrCast(state),
|
||||
|
||||
.actions = &.{
|
||||
.deallocate = ErasedActions.deallocate,
|
||||
.reallocate = ErasedActions.reallocate,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue