From 52645d06e1f3e46ac0d1d3a3441e1e8aa669fba9 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 9 Nov 2019 12:11:12 +1100 Subject: [PATCH 01/12] std: fix unfinished doc-comment in fifo --- lib/std/fifo.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index 22da02c8af3a26654175f502a41779399af9c010..7cf3f9e9060d28c90f0088abeb6a5a01d86d2f7d 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -193,7 +193,8 @@ pub fn FixedSizeFifo(comptime T: type) type { self.count += count; } - /// Appends the data in `src` to the fifo. You must + /// Appends the data in `src` to the fifo. + /// You must have ensured there is enough space. pub fn writeAssumeCapacity(self: *Self, src: []const T) void { assert(self.writableLength() >= src.len); -- 2.54.0 From 01b2a56225038537f525f52b0d9c821ffa77b90b Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 11 Nov 2019 01:32:52 +1100 Subject: [PATCH 02/12] std: move auto_align constant to top of comptime function At a later point in time this might be made into a parameter --- lib/std/fifo.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index 7cf3f9e9060d28c90f0088abeb6a5a01d86d2f7d..e357e23494b38c67769d0ebeb823ad4afbf8a0ad 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -10,6 +10,8 @@ const assert = debug.assert; const testing = std.testing; pub fn FixedSizeFifo(comptime T: type) type { + const autoalign = false; + return struct { allocator: *Allocator, buf: []T, @@ -107,8 +109,6 @@ pub fn FixedSizeFifo(comptime T: type) type { return self.readableSliceMut(offset); } - const autoalign = false; - /// Discard first `count` bytes of readable data pub fn discard(self: *Self, count: usize) void { assert(count <= self.count); -- 2.54.0 From e810f485ab6395357febc85b65afddae5b061955 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 11 Nov 2019 00:26:40 +1100 Subject: [PATCH 03/12] std: add optimization to fifo if size is power of two --- lib/std/fifo.zig | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index e357e23494b38c67769d0ebeb823ad4afbf8a0ad..04c681fcf19f754829f8d463c314c34a49de845c 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -12,6 +12,8 @@ const testing = std.testing; pub fn FixedSizeFifo(comptime T: type) type { const autoalign = false; + const powers_of_two = true; + return struct { allocator: *Allocator, buf: []T, @@ -68,10 +70,10 @@ pub fn FixedSizeFifo(comptime T: type) type { } /// Ensure that the buffer can fit at least `size` items - pub fn ensureCapacity(self: *Self, size: usize) error{OutOfMemory}!void { + pub fn ensureCapacity(self: *Self, size: usize) !void { if (self.buf.len >= size) return; self.realign(); - const new_size = math.ceilPowerOfTwo(usize, size) catch return error.OutOfMemory; + const new_size = if (powers_of_two) math.ceilPowerOfTwo(usize, size) catch return error.OutOfMemory else size; self.buf = try self.allocator.realloc(self.buf, new_size); } @@ -124,10 +126,19 @@ pub fn FixedSizeFifo(comptime T: type) type { @memset(unused2.ptr, undefined, unused2.len); } } - self.head = (self.head + count) % self.buf.len; - self.count -= count; - if (autoalign and self.count == 0) + if (autoalign and self.count == count) { self.head = 0; + self.count = 0; + } else { + var head = self.head + count; + if (powers_of_two) { + head &= self.buf.len - 1; + } else { + head %= self.buf.len; + } + self.head = head; + self.count -= count; + } } /// Read the next item from the fifo @@ -225,7 +236,13 @@ pub fn FixedSizeFifo(comptime T: type) type { fn rewind(self: *Self, size: usize) void { assert(self.writableLength() >= size); - self.head = (self.head + (self.buf.len - size)) % self.buf.len; + var head = self.head + (self.buf.len - size); + if (powers_of_two) { + head &= self.buf.len - 1; + } else { + head %= self.buf.len; + } + self.head = head; self.count += size; } @@ -246,7 +263,13 @@ pub fn FixedSizeFifo(comptime T: type) type { if (offset >= self.count) return error.EndOfStream; - return self.buf[(self.head + offset) % self.buf.len]; + var index = self.head + offset; + if (powers_of_two) { + index &= self.buf.len - 1; + } else { + index %= self.buf.len; + } + return self.buf[index]; } }; } -- 2.54.0 From c0e47cb645a96e8b80ba6f93e87ad276853570e0 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 11 Nov 2019 00:36:58 +1100 Subject: [PATCH 04/12] std: fix fifo for non-u8 types --- lib/std/fifo.zig | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index 04c681fcf19f754829f8d463c314c34a49de845c..198d1f2f676969414cfcd5c8c47865a4dd435a01 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -228,9 +228,14 @@ pub fn FixedSizeFifo(comptime T: type) type { return self.writeAssumeCapacity(src); } - pub fn print(self: *Self, comptime format: []const u8, args: ...) !void { - return std.fmt.format(self, error{OutOfMemory}, Self.write, format, args); - } + pub usingnamespace if (T == u8) + struct { + pub fn print(self: *Self, comptime format: []const u8, args: ...) !void { + return std.fmt.format(self, error{OutOfMemory}, Self.write, format, args); + } + } + else + struct {}; /// Make `count` bytes available before the current read location fn rewind(self: *Self, size: usize) void { @@ -340,3 +345,21 @@ test "ByteFifo" { testing.expectEqual(@as(usize, 0), fifo.readableLength()); } } + +test "FixedSizeFifo" { + inline for ([_]type{ u1, u8, u16, u64 }) |T| { + var fifo = FixedSizeFifo(T).init(debug.global_allocator); + defer fifo.deinit(); + + try fifo.write([_]T{ 0, 1, 1, 0, 1 }); + testing.expectEqual(@as(usize, 5), fifo.readableLength()); + + { + testing.expectEqual(@as(T, 0), try fifo.readItem()); + testing.expectEqual(@as(T, 1), try fifo.readItem()); + testing.expectEqual(@as(T, 1), try fifo.readItem()); + testing.expectEqual(@as(T, 0), try fifo.readItem()); + testing.expectEqual(@as(T, 1), try fifo.readItem()); + } + } +} -- 2.54.0 From cd749e04161a1a3899ed0a723a5cde444159900e Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 11 Nov 2019 01:23:21 +1100 Subject: [PATCH 05/12] std: fifo now has 3 modes: Static, Slice and Dynamic --- lib/std/fifo.zig | 133 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 96 insertions(+), 37 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index 198d1f2f676969414cfcd5c8c47865a4dd435a01..f7c7f7e1aea22c68bba103612be1a81cf7680bf5 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -9,30 +9,77 @@ const debug = std.debug; const assert = debug.assert; const testing = std.testing; -pub fn FixedSizeFifo(comptime T: type) type { +pub const FifoBufferType = union(enum) { + /// The buffer is internal to the fifo; it is of the specified size. + Static: usize, + + /// The buffer is passed as a slice to the initialiser. + Slice, + + /// The buffer is managed dynamically using a `mem.Allocator`. + Dynamic, +}; + +pub fn FixedSizeFifo( + comptime T: type, + comptime buffer_type: FifoBufferType, +) type { const autoalign = false; - const powers_of_two = true; + const powers_of_two = switch (buffer_type) { + .Static => std.math.isPowerOfTwo(buffer_type.Static), + .Slice => false, // Any size slice could be passed in + .Dynamic => true, // This could be configurable in future + }; return struct { - allocator: *Allocator, - buf: []T, + allocator: if (buffer_type == .Dynamic) *Allocator else void, + buf: if (buffer_type == .Static) [buffer_type.Static]T else []T, head: usize, count: usize, const Self = @This(); - pub fn init(allocator: *Allocator) Self { - return Self{ - .allocator = allocator, - .buf = [_]T{}, - .head = 0, - .count = 0, - }; - } + // Type of Self argument for slice operations. + // If buffer is inline (Static) then we need to ensure we haven't + // returned a slice into a copy on the stack + const SliceSelfArg = if (buffer_type == .Static) *Self else Self; + + pub usingnamespace switch (buffer_type) { + .Static => struct { + pub fn init() Self { + return .{ + .allocator = {}, + .buf = undefined, + .head = 0, + .count = 0, + }; + } + }, + .Slice => struct { + pub fn init(buf: []T) Self { + return .{ + .allocator = {}, + .buf = buf, + .head = 0, + .count = 0, + }; + } + }, + .Dynamic => struct { + pub fn init(allocator: *Allocator) Self { + return .{ + .allocator = allocator, + .buf = [_]T{}, + .head = 0, + .count = 0, + }; + } + }, + }; pub fn deinit(self: *Self) void { - self.allocator.free(self.buf); + if (buffer_type == .Dynamic) self.allocator.free(self.buf); self.* = undefined; } @@ -63,18 +110,24 @@ pub fn FixedSizeFifo(comptime T: type) type { /// Reduce allocated capacity to `size`. pub fn shrink(self: *Self, size: usize) void { assert(size >= self.count); - self.realign(); - self.buf = self.allocator.realloc(self.buf, size) catch |e| switch (e) { - error.OutOfMemory => return, // no problem, capacity is still correct then. - }; + if (buffer_type == .Dynamic) { + self.realign(); + self.buf = self.allocator.realloc(self.buf, size) catch |e| switch (e) { + error.OutOfMemory => return, // no problem, capacity is still correct then. + }; + } } /// Ensure that the buffer can fit at least `size` items pub fn ensureCapacity(self: *Self, size: usize) !void { if (self.buf.len >= size) return; - self.realign(); - const new_size = if (powers_of_two) math.ceilPowerOfTwo(usize, size) catch return error.OutOfMemory else size; - self.buf = try self.allocator.realloc(self.buf, new_size); + if (buffer_type == .Dynamic) { + self.realign(); + const new_size = if (powers_of_two) math.ceilPowerOfTwo(usize, size) catch return error.OutOfMemory else size; + self.buf = try self.allocator.realloc(self.buf, new_size); + } else { + return error.OutOfMemory; + } } /// Makes sure at least `size` items are unused @@ -90,7 +143,7 @@ pub fn FixedSizeFifo(comptime T: type) type { } /// Returns a writable slice from the 'read' end of the fifo - fn readableSliceMut(self: Self, offset: usize) []T { + fn readableSliceMut(self: SliceSelfArg, offset: usize) []T { if (offset > self.count) return [_]T{}; const start = self.head + offset; @@ -107,7 +160,7 @@ pub fn FixedSizeFifo(comptime T: type) type { } /// Returns a readable slice from `offset` - pub fn readableSlice(self: Self, offset: usize) []const T { + pub fn readableSlice(self: SliceSelfArg, offset: usize) []const T { return self.readableSliceMut(offset); } @@ -173,7 +226,7 @@ pub fn FixedSizeFifo(comptime T: type) type { /// Returns the first section of writable buffer /// Note that this may be of length 0 - pub fn writableSlice(self: Self, offset: usize) []T { + pub fn writableSlice(self: SliceSelfArg, offset: usize) []T { if (offset > self.buf.len) return [_]T{}; const tail = self.head + offset + self.count; @@ -279,10 +332,8 @@ pub fn FixedSizeFifo(comptime T: type) type { }; } -const ByteFifo = FixedSizeFifo(u8); - -test "ByteFifo" { - var fifo = ByteFifo.init(debug.global_allocator); +test "FixedSizeFifo(u8, .Dynamic)" { + var fifo = FixedSizeFifo(u8, .Dynamic).init(debug.global_allocator); defer fifo.deinit(); try fifo.write("HELLO"); @@ -348,18 +399,26 @@ test "ByteFifo" { test "FixedSizeFifo" { inline for ([_]type{ u1, u8, u16, u64 }) |T| { - var fifo = FixedSizeFifo(T).init(debug.global_allocator); - defer fifo.deinit(); + inline for ([_]FifoBufferType{ FifoBufferType{ .Static = 32 }, .Slice, .Dynamic }) |bt| { + const FifoType = FixedSizeFifo(T, bt); + var buf: if (bt == .Slice) [32]T else void = undefined; + var fifo = switch (bt) { + .Static => FifoType.init(), + .Slice => FifoType.init(buf[0..]), + .Dynamic => FifoType.init(debug.global_allocator), + }; + defer fifo.deinit(); - try fifo.write([_]T{ 0, 1, 1, 0, 1 }); - testing.expectEqual(@as(usize, 5), fifo.readableLength()); + try fifo.write([_]T{ 0, 1, 1, 0, 1 }); + testing.expectEqual(@as(usize, 5), fifo.readableLength()); - { - testing.expectEqual(@as(T, 0), try fifo.readItem()); - testing.expectEqual(@as(T, 1), try fifo.readItem()); - testing.expectEqual(@as(T, 1), try fifo.readItem()); - testing.expectEqual(@as(T, 0), try fifo.readItem()); - testing.expectEqual(@as(T, 1), try fifo.readItem()); + { + testing.expectEqual(@as(T, 0), try fifo.readItem()); + testing.expectEqual(@as(T, 1), try fifo.readItem()); + testing.expectEqual(@as(T, 1), try fifo.readItem()); + testing.expectEqual(@as(T, 0), try fifo.readItem()); + testing.expectEqual(@as(T, 1), try fifo.readItem()); + } } } } -- 2.54.0 From 6037f892125fc87996450f3f985b0d2eb38765c0 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 11 Nov 2019 01:25:38 +1100 Subject: [PATCH 06/12] std: fifo rename from FixedSizeFifo to LinearFifo --- lib/std/fifo.zig | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index f7c7f7e1aea22c68bba103612be1a81cf7680bf5..a8ba9f6ffe8a9d89c12664c00348fc78a6fade13 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -9,7 +9,7 @@ const debug = std.debug; const assert = debug.assert; const testing = std.testing; -pub const FifoBufferType = union(enum) { +pub const LinearFifoBufferType = union(enum) { /// The buffer is internal to the fifo; it is of the specified size. Static: usize, @@ -20,9 +20,9 @@ pub const FifoBufferType = union(enum) { Dynamic, }; -pub fn FixedSizeFifo( +pub fn LinearFifo( comptime T: type, - comptime buffer_type: FifoBufferType, + comptime buffer_type: LinearFifoBufferType, ) type { const autoalign = false; @@ -332,8 +332,8 @@ pub fn FixedSizeFifo( }; } -test "FixedSizeFifo(u8, .Dynamic)" { - var fifo = FixedSizeFifo(u8, .Dynamic).init(debug.global_allocator); +test "LinearFifo(u8, .Dynamic)" { + var fifo = LinearFifo(u8, .Dynamic).init(debug.global_allocator); defer fifo.deinit(); try fifo.write("HELLO"); @@ -397,10 +397,10 @@ test "FixedSizeFifo(u8, .Dynamic)" { } } -test "FixedSizeFifo" { +test "LinearFifo" { inline for ([_]type{ u1, u8, u16, u64 }) |T| { - inline for ([_]FifoBufferType{ FifoBufferType{ .Static = 32 }, .Slice, .Dynamic }) |bt| { - const FifoType = FixedSizeFifo(T, bt); + inline for ([_]LinearFifoBufferType{ LinearFifoBufferType{ .Static = 32 }, .Slice, .Dynamic }) |bt| { + const FifoType = LinearFifo(T, bt); var buf: if (bt == .Slice) [32]T else void = undefined; var fifo = switch (bt) { .Static => FifoType.init(), -- 2.54.0 From 61179a4d5210399107c2964f02071cb55264bba2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 11 Nov 2019 02:46:40 +1100 Subject: [PATCH 07/12] std: follow zig standard library convention and have fifo.read number of items --- lib/std/fifo.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index a8ba9f6ffe8a9d89c12664c00348fc78a6fade13..87d0ce4e88cfb7b16c797af031f216aeaa71e136 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -203,8 +203,8 @@ pub fn LinearFifo( return c; } - /// Read data from the fifo into `dst`, returns slice of bytes copied (subslice of `dst`) - pub fn read(self: *Self, dst: []T) []T { + /// Read data from the fifo into `dst`, returns number of bytes copied. + pub fn read(self: *Self, dst: []T) usize { var dst_left = dst; while (dst_left.len > 0) { @@ -216,7 +216,7 @@ pub fn LinearFifo( dst_left = dst_left[n..]; } - return dst[0 .. dst.len - dst_left.len]; + return dst.len - dst_left.len; } /// Returns number of bytes available in fifo @@ -384,7 +384,7 @@ test "LinearFifo(u8, .Dynamic)" { { try fifo.unget("prependedstring"); var result: [30]u8 = undefined; - testing.expectEqualSlices(u8, "prependedstringabcdefghij", fifo.read(&result)); + testing.expectEqualSlices(u8, "prependedstringabcdefghij", result[0..fifo.read(&result)]); } fifo.shrink(0); @@ -392,7 +392,7 @@ test "LinearFifo(u8, .Dynamic)" { { try fifo.print("{}, {}!", "Hello", "World"); var result: [30]u8 = undefined; - testing.expectEqualSlices(u8, "Hello, World!", fifo.read(&result)); + testing.expectEqualSlices(u8, "Hello, World!", result[0..fifo.read(&result)]); testing.expectEqual(@as(usize, 0), fifo.readableLength()); } } -- 2.54.0 From c393969a2013fc64f1e1c65157164dfdfbafb08a Mon Sep 17 00:00:00 2001 From: daurnimator Date: Mon, 11 Nov 2019 03:03:57 +1100 Subject: [PATCH 08/12] std: fix bug in fifo.unget if rewinding doesn't wrap around --- lib/std/fifo.zig | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index 87d0ce4e88cfb7b16c797af031f216aeaa71e136..9fe46fedad1adc70e91a9e406269772e56befeda 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -311,9 +311,13 @@ pub fn LinearFifo( self.rewind(src.len); const slice = self.readableSliceMut(0); - mem.copy(T, slice, src[0..slice.len]); - const slice2 = self.readableSliceMut(slice.len); - mem.copy(T, slice2, src[slice.len..]); + if (src.len < slice.len) { + mem.copy(T, slice, src); + } else { + mem.copy(T, slice, src[0..slice.len]); + const slice2 = self.readableSliceMut(slice.len); + mem.copy(T, slice2, src[slice.len..]); + } } /// Peek at the item at `offset` @@ -385,6 +389,9 @@ test "LinearFifo(u8, .Dynamic)" { try fifo.unget("prependedstring"); var result: [30]u8 = undefined; testing.expectEqualSlices(u8, "prependedstringabcdefghij", result[0..fifo.read(&result)]); + try fifo.unget("b"); + try fifo.unget("a"); + testing.expectEqualSlices(u8, "ab", result[0..fifo.read(&result)]); } fifo.shrink(0); -- 2.54.0 From 3062e0e9320a3ce9447e4611919cbe93296a1ce1 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 16 Nov 2019 22:15:06 +1100 Subject: [PATCH 09/12] std: add fifo.writeItem --- lib/std/fifo.zig | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index 9fe46fedad1adc70e91a9e406269772e56befeda..de8e7a55de04980a552dd1aac884855d5006f72e 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -273,6 +273,20 @@ pub fn LinearFifo( } } + /// Write a single item to the fifo + pub fn writeItem(self: *Self, item: T) !void { + try self.ensureUnusedCapacity(1); + + var tail = self.head + self.count; + if (powers_of_two) { + tail &= self.buf.len - 1; + } else { + tail %= self.buf.len; + } + self.buf[tail] = byte; + self.update(1); + } + /// Appends the data in `src` to the fifo. /// Allocates more memory as necessary pub fn write(self: *Self, src: []const T) !void { -- 2.54.0 From b4091e3aec1e13350983f8f8df31280b285fe111 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 23 Nov 2019 17:57:17 +1100 Subject: [PATCH 10/12] std: fifo.deinit didn't need to take a pointer --- lib/std/fifo.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index de8e7a55de04980a552dd1aac884855d5006f72e..bb9d1b2a1de732440fb664ebe7af11a706a9849b 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -78,9 +78,8 @@ pub fn LinearFifo( }, }; - pub fn deinit(self: *Self) void { + pub fn deinit(self: Self) void { if (buffer_type == .Dynamic) self.allocator.free(self.buf); - self.* = undefined; } pub fn realign(self: *Self) void { -- 2.54.0 From 94485b2a58f8efbfb61e46d2efa257420f12a22d Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 23 Nov 2019 13:13:47 +1100 Subject: [PATCH 11/12] std: clean up fifo.readableSliceMut --- lib/std/fifo.zig | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index bb9d1b2a1de732440fb664ebe7af11a706a9849b..0b1b881613a98afc26d47bb7436ca0a8d42e80a0 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -145,16 +145,13 @@ pub fn LinearFifo( fn readableSliceMut(self: SliceSelfArg, offset: usize) []T { if (offset > self.count) return [_]T{}; - const start = self.head + offset; + var start = self.head + offset; if (start >= self.buf.len) { - return self.buf[start - self.buf.len ..][0 .. self.count - offset]; + start -= self.buf.len; + return self.buf[start..self.count - offset]; } else { - const end: usize = self.head + self.count; - if (end >= self.buf.len) { - return self.buf[start..self.buf.len]; - } else { - return self.buf[start..end]; - } + const end = math.min(self.head + self.count, self.buf.len); + return self.buf[start..end]; } } -- 2.54.0 From 1a84bcefb658b45103155725067e0820dd4243a0 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 23 Nov 2019 18:42:22 +1100 Subject: [PATCH 12/12] std: fix mismatched doc-comment/argument names in fifo.rewind --- lib/std/fifo.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/std/fifo.zig b/lib/std/fifo.zig index 0b1b881613a98afc26d47bb7436ca0a8d42e80a0..12d1750df3360a53b64b187ee9f69a3125e2b8a0 100644 --- a/lib/std/fifo.zig +++ b/lib/std/fifo.zig @@ -300,18 +300,18 @@ pub fn LinearFifo( else struct {}; - /// Make `count` bytes available before the current read location - fn rewind(self: *Self, size: usize) void { - assert(self.writableLength() >= size); + /// Make `count` items available before the current read location + fn rewind(self: *Self, count: usize) void { + assert(self.writableLength() >= count); - var head = self.head + (self.buf.len - size); + var head = self.head + (self.buf.len - count); if (powers_of_two) { head &= self.buf.len - 1; } else { head %= self.buf.len; } self.head = head; - self.count += size; + self.count += count; } /// Place data back into the read stream -- 2.54.0