Compare commits

...

3 Commits

Author SHA1 Message Date
kayomn fcd4ecd85d Refactor dynamic memory allocation interface
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/pr Build is failing Details
2022-11-02 15:15:18 +00:00
kayomn 2e544393a5 Simplify programming interface for creating closureless Functions 2022-11-02 13:11:17 +00:00
kayomn eb4a758251 Make Function closures only accept a pointer context 2022-11-02 11:27:10 +00:00
4 changed files with 124 additions and 93 deletions

View File

@ -3,73 +3,53 @@ const meta = @import("./meta.zig");
const stack = @import("./stack.zig");
const testing = @import("./testing.zig");
///
///
///
pub const Allocation = struct {
existing: ?[*]u8,
size: usize
};
///
/// Dynamic memory allocation interface.
///
pub const Allocator = union (enum) {
bound: struct {
alloc: fn (usize) ?[*]u8,
dealloc: fn ([*]u8) void,
},
pub const Allocator = meta.Function(Allocation, ?[*]u8);
arena: struct {
buffer: []u8,
cursor: usize = 0,
},
///
///
///
pub const ArenaAllocator = struct {
region: []u8,
cursor: usize = 0,
///
/// [MakeError.OutOfMemory] is used to indicate there is not enough memory available for a given
/// operation.
///
pub const MakeError = error {
OutOfMemory,
};
///
/// Frees `allocation` using `allocator`.
///
pub fn free(allocator: *Allocator, allocation: anytype) void {
switch (@typeInfo(@TypeOf(allocation))) {
.Pointer => |pointer| if (pointer.size == .Slice)
@compileError("`allocation` cannot be a slice"),
pub fn allocator(arena_allocator: *ArenaAllocator) Allocator {
return Allocator.fromClosure(arena_allocator, struct {
fn call(context: *ArenaAllocator, allocation: Allocation) ?[*]u8 {
_ = allocation;
_ = context;
else => @compileError("`allocation` must be a pointer"),
}
if (@typeInfo(@TypeOf(allocation)) != .Pointer)
@compileError("`allocation` must be a pointer");
// TODO: Implement arena de-allocation.
switch (allocator.*) {
.bound => |bound| bound.dealloc(@ptrCast([*]u8, allocation)),
.arena => {},
}
return null;
}
}.call);
}
};
///
/// Attempts to allocate `size` number of `Element`s using `allocator`.
///
/// Returns the allocation or a [MakeError] if it failed.
///
pub fn make(allocator: *Allocator, comptime Element: type, size: usize) MakeError![*]Element {
switch (allocator.*) {
.bound => |bound| return @ptrCast([*]Element, @alignCast(@alignOf(Element),
bound.alloc(@sizeOf(Element) * size) orelse return error.OutOfMemory)),
.arena => |*stack| {
defer stack.cursor += size;
return @ptrCast([*]Element, @alignCast(@alignOf(Element), stack.buffer.ptr));
},
}
}
///
///
///
pub const MakeError = error {
OutOfMemory,
};
///
/// Closure that captures a reference to readable resources like block devices, memory buffers,
/// network sockets, and more.
///
pub const Reader = meta.Function(@sizeOf(usize), []u8, usize);
pub const Reader = meta.Function([]u8, usize);
///
/// Returns a state machine for lazily computing all `Element` components of a given source input
@ -187,7 +167,7 @@ test "Spliterator of string literals" {
/// Closure that captures a reference to writable resources like block devices, memory buffers,
/// network sockets, and more.
///
pub const Writer = meta.Function(@sizeOf(usize), []const u8, usize);
pub const Writer = meta.Function([]const u8, usize);
///
/// Returns a sliced reference of the raw bytes in `pointer`.
@ -337,6 +317,24 @@ test "Find first of sequence" {
try testing.expect(findFirstOf([]const u8, haystack, &.{"baz", "bar"}, testEquality) == null);
}
///
///
///
pub fn free(allocator: Allocator, allocated_memory: anytype) void {
if (allocator.call(.{
.existing = @ptrCast([*]u8, switch (@typeInfo(@TypeOf(allocated_memory))) {
.Pointer => |info| switch (info.size) {
.One, .Many, .C => allocated_memory,
.Slice => allocated_memory.ptr,
},
else => @compileError("`allocated_memory` must be a pointer"),
}),
.size = 0,
}) != null) unreachable;
}
///
/// Returns a deterministic hash code compiled from each byte in `bytes`.
///
@ -357,6 +355,16 @@ test "Hashing bytes" {
try testing.expect(hashBytes(bytes_sequence) != hashBytes(&.{69, 42}));
}
///
///
///
pub fn makeMany(comptime Element: type, allocator: Allocator, size: usize) MakeError![*]Element {
return @ptrCast([*]Element, @alignCast(@alignOf(Element), allocator.call(.{
.existing = null,
.size = size,
}) orelse return error.OutOfMemory));
}
///
/// Swaps the `Data` in `this` with `that`.
///
@ -377,21 +385,19 @@ test "Data swapping" {
}
///
/// Returns a [Writer] that silently consumes all given data without failure and throws it away.
/// [Writer] that silently consumes all given data without failure and throws it away.
///
/// This is commonly used for testing or redirected otherwise unwanted output data that has to be
/// sent somewhere for whatever reason.
///
pub fn nullWriter() Writer {
return Writer.capture(@as(usize, 0), struct {
fn write(_: usize, buffer: []const u8) usize {
return buffer.len;
}
}.write);
}
pub const null_writer = Writer.from(struct {
fn write(buffer: []const u8) usize {
return buffer.len;
}
}.write);
test "Null writing" {
const sequence = "foo";
try testing.expect(nullWriter().call(sequence) == sequence.len);
try testing.expect(null_writer.call(sequence) == sequence.len);
}

View File

@ -13,10 +13,14 @@ pub fn FnReturn(comptime Fn: type) type {
/// Returns a single-input single-output closure type where `In` represents the input type, `Out`
/// represents the output type, and `captures_size` represents the size of the closure context.
///
pub fn Function(comptime captures_size: usize, comptime In: type, comptime Out: type) type {
pub fn Function(comptime In: type, comptime Out: type) type {
return struct {
callErased: fn (*anyopaque, In) Out,
context: [captures_size]u8,
context: *anyopaque,
fn Invoker(comptime Context: type) type {
return if (Context == void) fn (In) Out else fn (Context, In) Out;
}
///
/// Function type.
@ -26,37 +30,55 @@ pub fn Function(comptime captures_size: usize, comptime In: type, comptime Out:
///
/// Invokes `self` with `input`, producing a result according to the current context data.
///
pub fn call(self: *Self, input: In) Out {
return self.callErased(&self.context, input);
pub fn call(self: Self, input: In) Out {
return self.callErased(self.context, input);
}
///
/// Creates a new [Self] by capturing the `captures` value as the context and `invoke` as
/// the as the behavior executed when [call] or [callErased] is called.
/// Creates and returns a [Self] using the `invoke` as the behavior executed when [call] or
/// [callErased] is called.
///
/// The newly created [Self] is returned.
/// For creating a closure-style function, see [fromClosure].
///
pub fn capture(captures: anytype, comptime invoke: fn (@TypeOf(captures), In) Out) Self {
const Captures = @TypeOf(captures);
if (@sizeOf(Captures) > captures_size)
@compileError("`captures` exceeds the size limit of the capture context");
const captures_align = @alignOf(Captures);
var function = Self{
pub fn from(comptime invoke: fn (In) Out) Self {
return .{
.context = undefined,
.callErased = struct {
fn callErased(erased: *anyopaque, input: In) Out {
return invoke(if (Captures == void) {} else @ptrCast(*Captures,
@alignCast(@alignOf(Captures), erased)).*, input);
fn callErased(_: *anyopaque, input: In) Out {
return invoke(input);
}
}.callErased,
};
}
if (Captures != void)
@ptrCast(*Captures, @alignCast(captures_align, &function.context)).* = captures;
///
/// Creates and returns a [Self] by capturing the `context` value as the capture context and
/// `invoke` as the behavior executed when [call] or [callErased] is called.
///
/// The newly created [Self] is returned.
///
pub fn fromClosure(context: anytype, comptime invoke: fn (@TypeOf(context), In) Out) Self {
const Context = @TypeOf(context);
switch (@typeInfo(Context)) {
.Pointer => |info| if (info.size == .Slice)
@compileError("`context` cannot be a slice"),
.Void => {},
else => @compileError("`context` must be a pointer"),
}
var function = Self{
.context = @ptrCast(*anyopaque, context),
.callErased = struct {
fn callErased(erased: *anyopaque, input: In) Out {
return if (Context == void) invoke(input) else invoke(@ptrCast(
*Context, @alignCast(@alignOf(Context), erased)).*, input);
}
}.callErased,
};
return function;
}

View File

@ -113,7 +113,7 @@ test "Fixed stack of string literals" {
///
/// Potential errors that may occur while trying to push one or more elements into a stack.
///
pub const PushError = io.Allocator.MakeError;
pub const PushError = io.MakeError;
///
/// Returns an [io.Writer] wrapping `fixed_stack`.
@ -122,7 +122,7 @@ pub const PushError = io.Allocator.MakeError;
/// referenced by `fixed_stack` until it is full.
///
pub fn fixedWriter(fixed_stack: *Fixed(u8)) io.Writer {
return io.Writer.capture(fixed_stack, struct {
return io.Writer.fromClosure(fixed_stack, struct {
fn write(stack: *Fixed(u8), buffer: []const u8) usize {
stack.pushAll(buffer) catch |err| switch (err) {
error.OutOfMemory => return 0,

View File

@ -11,7 +11,7 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
const Allocator = io.Allocator;
return struct {
allocator: *Allocator,
allocator: Allocator,
load_limit: f32,
buckets: []Bucket,
filled: usize,
@ -37,7 +37,7 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
/// Deinitializes `self`, preventing any further use.
///
pub fn deinit(self: *Self) void {
self.allocator.free(self.buckets.ptr);
io.free(self.allocator, self.buckets);
self.buckets = &.{};
}
@ -45,13 +45,13 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
///
/// Initializes a [Self] using `allocator` as the memory allocation strategy.
///
/// Returns a new [Self] value or an [io.Allocator.MakeError] if initializing failed.
/// Returns a new [Self] value or an [io.MakeError] if initializing failed.
///
pub fn init(allocator: *Allocator) Allocator.MakeError!Self {
pub fn init(allocator: Allocator) io.MakeError!Self {
const initial_capacity = 4;
return Self{
.buckets = (try allocator.make(Bucket, initial_capacity))[0 .. initial_capacity],
.buckets = (try io.makeMany(Bucket, allocator, initial_capacity))[0 .. initial_capacity],
.filled = 0,
.allocator = allocator,
.load_limit = 0.75,
@ -99,11 +99,12 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
if (self.loadFactor() >= self.load_limit) {
const old_buckets = self.buckets;
defer self.allocator.free(old_buckets.ptr);
defer io.free(self.allocator, old_buckets);
const bucket_count = old_buckets.len * 2;
self.buckets = (try self.allocator.make(Bucket, bucket_count))[0 .. bucket_count];
self.buckets = (try io.makeMany(Bucket, self.allocator,
bucket_count))[0 .. bucket_count];
for (old_buckets) |bucket, index| self.buckets[index] = bucket;
}
@ -163,7 +164,7 @@ pub fn Hashed(comptime Key: type, comptime Value: type,
/// [InsertError.KeyExists] occurs when an insertion was attempted on a table with a matching key
/// already present.
///
pub const InsertError = io.Allocator.MakeError || error {
pub const InsertError = io.MakeError || error {
KeyExists,
};
@ -197,8 +198,10 @@ pub const string_literal_context = KeyContext([]const u8){
test "Hash table manipulation with string literal context" {
var buffer = [_]u8{0} ** 1024;
var allocator = io.Allocator{.arena = .{.buffer = &buffer}};
var table = try Hashed([]const u8, u32, string_literal_context).init(&allocator);
var arena_allocator = io.ArenaAllocator{.region = &buffer};
var table =
try Hashed([]const u8, u32, string_literal_context).init(arena_allocator.allocator());
defer table.deinit();