diff options
Diffstat (limited to 'src/zisp/lib/seg_stack.zig')
| -rw-r--r-- | src/zisp/lib/seg_stack.zig | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/zisp/lib/seg_stack.zig b/src/zisp/lib/seg_stack.zig index 0867892..d2c6aaa 100644 --- a/src/zisp/lib/seg_stack.zig +++ b/src/zisp/lib/seg_stack.zig @@ -5,8 +5,13 @@ const std = @import("std"); const Alloc = std.mem.Allocator; /// Segmented linked list backed stack -pub fn SegStack(T: type, seg_max_bytes: usize) type { - const seg_size: usize = (seg_max_bytes - @sizeOf(usize)) / @sizeOf(T); +pub fn SegStack(T: type, seg_max_bytes: comptime_int) type { + + // Each Node has a pointer (= usize) and the element array; calculate the + // correct segment size (as in element count) based on that. + const seg_size = (seg_max_bytes - @sizeOf(usize)) / @sizeOf(T); + + const IdxType = std.math.IntFittingRange(seg_size); comptime { if (seg_size < 4) { @@ -31,7 +36,7 @@ pub fn SegStack(T: type, seg_max_bytes: usize) type { const Self = @This(); head: *Node, - idx: usize = 0, + idx: IdxType = 0, // To prevent "thrashing" we don't immediately deallocate the current // node when it's emptied but rather save it aside. |
