Skip to content

Commit 267f6c1

Browse files
author
dweiller
committed
std.RingBuffer: add (non-concurrent) RingBuffer implementation
1 parent 4935173 commit 267f6c1

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

lib/std/compress/zstandard/RingBuffer.zig renamed to lib/std/RingBuffer.zig

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
//! This ring buffer stores read and write indices while being able to utilise the full
2-
//! backing slice by incrementing the indices modulo twice the slice's length and reducing
3-
//! indices modulo the slice's length on slice access. This means that whether the ring buffer
4-
//! if full or empty can be distinguised by looking at the different between the read and write
5-
//! indices without adding an extra boolean flag or having to reserve a slot in the buffer.
1+
//! This ring buffer stores read and write indices while being able to utilise
2+
//! the full backing slice by incrementing the indices modulo twice the slice's
3+
//! length and reducing indices modulo the slice's length on slice access. This
4+
//! means that whether the ring buffer if full or empty can be distinguished by
5+
//! looking at the difference between the read and write indices without adding
6+
//! an extra boolean flag or having to reserve a slot in the buffer.
7+
//!
8+
//! This ring buffer has not been implemented with thread safety in mind, and
9+
//! therefore should not be assumed to be suitable for use cases involving
10+
//! separate reader and writer threads.
611

712
const Allocator = @import("std").mem.Allocator;
813
const assert = @import("std").debug.assert;
@@ -15,7 +20,7 @@ write_index: usize,
1520

1621
pub const Error = error{Full};
1722

18-
/// Allocate a new `RingBuffer`
23+
/// Allocate a new `RingBuffer`; `deinit()` should be called to free the buffer.
1924
pub fn init(allocator: Allocator, capacity: usize) Allocator.Error!RingBuffer {
2025
const bytes = try allocator.alloc(u8, capacity);
2126
return RingBuffer{
@@ -25,7 +30,8 @@ pub fn init(allocator: Allocator, capacity: usize) Allocator.Error!RingBuffer {
2530
};
2631
}
2732

28-
/// Free a `RingBuffer`
33+
/// Free the data backing a `RingBuffer`; must be passed the same `Allocator` as
34+
/// `init()`.
2935
pub fn deinit(self: *RingBuffer, allocator: Allocator) void {
3036
allocator.free(self.data);
3137
self.* = undefined;
@@ -36,7 +42,7 @@ pub fn mask(self: RingBuffer, index: usize) usize {
3642
return index % self.data.len;
3743
}
3844

39-
/// Returns `index` module twice the length of the backing slice.
45+
/// Returns `index` modulo twice the length of the backing slice.
4046
pub fn mask2(self: RingBuffer, index: usize) usize {
4147
return index % (2 * self.data.len);
4248
}
@@ -55,7 +61,7 @@ pub fn writeAssumeCapacity(self: *RingBuffer, byte: u8) void {
5561
self.write_index = self.mask2(self.write_index + 1);
5662
}
5763

58-
/// Write `bytes` into the ring bufffer. Returns `error.Full` if the ring
64+
/// Write `bytes` into the ring buffer. Returns `error.Full` if the ring
5965
/// buffer does not have enough space, without writing any data.
6066
pub fn writeSlice(self: *RingBuffer, bytes: []const u8) Error!void {
6167
if (self.len() + bytes.len > self.data.len) return error.Full;
@@ -72,6 +78,13 @@ pub fn writeSliceAssumeCapacity(self: *RingBuffer, bytes: []const u8) void {
7278
/// ring buffer is empty.
7379
pub fn read(self: *RingBuffer) ?u8 {
7480
if (self.isEmpty()) return null;
81+
return self.readAssumeLength();
82+
}
83+
84+
/// Consume a byte from the ring buffer and return it; asserts that the buffer
85+
/// is not empty.
86+
pub fn readAssumeLength(self: *RingBuffer) u8 {
87+
assert(!self.isEmpty());
7588
const byte = self.data[self.mask(self.read_index)];
7689
self.read_index = self.mask2(self.read_index + 1);
7790
return byte;
@@ -95,15 +108,15 @@ pub fn len(self: RingBuffer) usize {
95108
}
96109

97110
/// A `Slice` represents a region of a ring buffer. The region is split into two
98-
/// sections as the ring buffer data will not be contiguous if the desired region
99-
/// wraps to the start of the backing slice.
111+
/// sections as the ring buffer data will not be contiguous if the desired
112+
/// region wraps to the start of the backing slice.
100113
pub const Slice = struct {
101114
first: []u8,
102115
second: []u8,
103116
};
104117

105-
/// Returns a `Slice` for the region of the ring buffer staring at `self.mask(start_unmasked)`
106-
/// with the specified length.
118+
/// Returns a `Slice` for the region of the ring buffer starting at
119+
/// `self.mask(start_unmasked)` with the specified length.
107120
pub fn sliceAt(self: RingBuffer, start_unmasked: usize, length: usize) Slice {
108121
assert(length <= self.data.len);
109122
const slice1_start = self.mask(start_unmasked);
@@ -117,6 +130,7 @@ pub fn sliceAt(self: RingBuffer, start_unmasked: usize, length: usize) Slice {
117130
}
118131

119132
/// Returns a `Slice` for the last `length` bytes written to the ring buffer.
133+
/// Does not check that any bytes have been written into the region.
120134
pub fn sliceLast(self: RingBuffer, length: usize) Slice {
121135
return self.sliceAt(self.write_index + self.data.len - length, length);
122136
}

lib/std/compress/zstandard.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const std = @import("std");
22
const Allocator = std.mem.Allocator;
3+
const RingBuffer = std.RingBuffer;
34

45
const types = @import("zstandard/types.zig");
56
pub const frame = types.frame;
67
pub const compressed_block = types.compressed_block;
78

8-
const RingBuffer = @import("zstandard/RingBuffer.zig");
99
pub const decompress = @import("zstandard/decompress.zig");
1010

1111
pub fn Stream(

lib/std/compress/zstandard/decode/block.zig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const std = @import("std");
22
const assert = std.debug.assert;
3+
const RingBuffer = std.RingBuffer;
34

45
const types = @import("../types.zig");
56
const frame = types.frame;
@@ -8,9 +9,6 @@ const LiteralsSection = types.compressed_block.LiteralsSection;
89
const SequencesSection = types.compressed_block.SequencesSection;
910

1011
const huffman = @import("huffman.zig");
11-
12-
const RingBuffer = @import("../RingBuffer.zig");
13-
1412
const readers = @import("../readers.zig");
1513

1614
const decodeFseTable = @import("fse.zig").decodeFseTable;

lib/std/compress/zstandard/decompress.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const std = @import("std");
22
const assert = std.debug.assert;
33
const Allocator = std.mem.Allocator;
4+
const RingBuffer = std.RingBuffer;
45

56
const types = @import("types.zig");
67
const frame = types.frame;
@@ -12,8 +13,6 @@ const Table = types.compressed_block.Table;
1213

1314
pub const block = @import("decode/block.zig");
1415

15-
pub const RingBuffer = @import("RingBuffer.zig");
16-
1716
const readers = @import("readers.zig");
1817

1918
const readInt = std.mem.readIntLittle;

lib/std/std.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub const PackedIntSliceEndian = @import("packed_int_array.zig").PackedIntSliceE
3131
pub const PriorityQueue = @import("priority_queue.zig").PriorityQueue;
3232
pub const PriorityDequeue = @import("priority_dequeue.zig").PriorityDequeue;
3333
pub const Progress = @import("Progress.zig");
34+
pub const RingBuffer = @import("RingBuffer.zig");
3435
pub const SegmentedList = @import("segmented_list.zig").SegmentedList;
3536
pub const SemanticVersion = @import("SemanticVersion.zig");
3637
pub const SinglyLinkedList = @import("linked_list.zig").SinglyLinkedList;

0 commit comments

Comments
 (0)