summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.zig6
-rw-r--r--src/c_heap.zig27
2 files changed, 27 insertions, 6 deletions
diff --git a/build.zig b/build.zig
index 5e88ef68..4485140d 100644
--- a/build.zig
+++ b/build.zig
@@ -1928,18 +1928,20 @@ pub fn build(b: *std.Build) void {
const history_step = b.step("history-test", "test historical comparisons and regression gates");
const protocol_match = testMatch(b, unit_profile, test_filters, protocol_step);
const history_match = testMatch(b, unit_profile, test_filters, history_step);
- for ([_]struct { path: []const u8, libc: bool }{
+ for ([_]struct { path: []const u8, libc: bool, optimize: ?std.builtin.OptimizeMode = null }{
.{ .path = "test/history.zig", .libc = false },
.{ .path = "src/9p.zig", .libc = false },
.{ .path = "src/esp32p4_gpio.zig", .libc = false },
.{ .path = "src/esp32p4/input_rescue.zig", .libc = false },
.{ .path = "src/fonts.zig", .libc = true },
.{ .path = "src/c_heap.zig", .libc = false },
+ // Its lookup goes wrong only where the optimizer folds it.
+ .{ .path = "src/c_heap.zig", .libc = false, .optimize = .ReleaseSafe },
}) |entry| {
const tests = b.addTest(.{
.root_module = b.createModule(.{
.target = target,
- .optimize = optimize,
+ .optimize = entry.optimize orelse optimize,
.root_source_file = b.path(entry.path),
.link_libc = entry.libc,
}),
diff --git a/src/c_heap.zig b/src/c_heap.zig
index 2a3e865f..efd2064d 100644
--- a/src/c_heap.zig
+++ b/src/c_heap.zig
@@ -39,21 +39,28 @@ pub fn Heap(comptime current: *const std.mem.Allocator) type {
var last: std.atomic.Value(usize) = .init(0);
pub fn malloc(size: usize) callconv(.c) ?*anyopaque {
+ // `current` as its bytes are: std's stateless allocators (c_,
+ // page_, smp_allocator) leave `ptr` undefined, and a heap's
+ // allocator never repointed is a constant to a release build,
+ // which then takes `ptr` for whatever it likes at each
+ // comparison below: never found, an entry a malloc, the 65th a
+ // panic (a box session's zstd after 15 far joins, 2026-10-30).
+ const now = @as(*const volatile std.mem.Allocator, current).*;
const total = std.math.add(usize, header, size) catch return null;
- const bytes = current.alignedAlloc(u8, .@"16", total) catch return null;
+ const bytes = now.alignedAlloc(u8, .@"16", total) catch return null;
var index = last.load(.monotonic);
if (index >= allocators_len.load(.acquire) or
- allocators[index].ptr != current.ptr or allocators[index].vtable != current.vtable)
+ allocators[index].ptr != now.ptr or allocators[index].vtable != now.vtable)
{
while (!allocators_lock.tryLock()) std.atomic.spinLoopHint();
defer allocators_lock.unlock();
const len = allocators_len.load(.monotonic);
index = for (allocators[0..len], 0..) |known, at| {
- if (known.ptr == current.ptr and known.vtable == current.vtable) break at;
+ if (known.ptr == now.ptr and known.vtable == now.vtable) break at;
} else len;
if (index == len) {
if (len == allocators.len) @panic("c_heap: more distinct allocators than it can name");
- allocators[len] = current.*;
+ allocators[len] = now;
allocators_len.store(len + 1, .release);
}
last.store(index, .monotonic);
@@ -133,3 +140,15 @@ test "a C heap block keeps its bytes, frees exactly, and goes back where it came
live = null;
heap.free(null);
}
+
+/// Never written, as a release session's zstd heap's allocator was: a
+/// constant to the compiler, `ptr` (undefined in std's stateless
+/// allocators) whatever it likes at each comparison.
+var stateless: std.mem.Allocator = std.heap.page_allocator;
+
+test "a heap over a stateless allocator that is never repointed names it once" {
+ const heap = Heap(&stateless);
+ const before = allocators_len.load(.monotonic);
+ for (0..2 * allocators.len) |_| heap.free(heap.malloc(8) orelse return error.OutOfMemory);
+ try std.testing.expect(allocators_len.load(.monotonic) <= before + 1);
+}