Tidy up allocator type erasing bind function
This commit is contained in:
parent
c1f174a513
commit
ed585278ff
|
@ -24,29 +24,30 @@ pub const Allocator = struct {
|
||||||
pub fn bind(comptime State: type, state: *State, comptime actions: Actions(State)) Allocator {
|
pub fn bind(comptime State: type, state: *State, comptime actions: Actions(State)) Allocator {
|
||||||
const is_zero_aligned = @alignOf(State) == 0;
|
const is_zero_aligned = @alignOf(State) == 0;
|
||||||
|
|
||||||
|
const ErasedActions = struct {
|
||||||
|
fn deallocate(context: *anyopaque, allocation: []Byte) void {
|
||||||
|
if (is_zero_aligned) {
|
||||||
|
return actions.deallocator(@ptrCast(context), allocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
return actions.deallocate(@ptrCast(@alignCast(context)), allocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return actions.reallocate(@ptrCast(@alignCast(context)), return_address, existing_allocation, size);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.context = if (is_zero_aligned) state else @ptrCast(state),
|
.context = if (is_zero_aligned) state else @ptrCast(state),
|
||||||
|
|
||||||
.actions = &.{
|
.actions = &.{
|
||||||
.deallocate = struct {
|
.deallocate = ErasedActions.deallocate,
|
||||||
fn deallocate(context: *anyopaque, allocation: []Byte) void {
|
.reallocate = ErasedActions.reallocate,
|
||||||
if (is_zero_aligned) {
|
|
||||||
return actions.deallocator(@ptrCast(context), allocation);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return actions.reallocate(@ptrCast(@alignCast(context)), return_address, existing_allocation, size);
|
|
||||||
}
|
|
||||||
}.reallocate,
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue