Commit ba58b7b881

David Rubin <daviru007@icloud.com>
2024-07-15 10:52:08
heap: create a work-around page-allocator
the risc-v backend doesn't have `@cmpxchg*` implemented and so it can't use the hint that the current page-allocator uses. this work-around branch can be removed when I implement the atomic built-in.
1 parent d3f7552
Changed files (1)
lib
lib/std/heap/PageAllocator.zig
@@ -34,6 +34,20 @@ fn alloc(_: *anyopaque, n: usize, log2_align: u8, ra: usize) ?[*]u8 {
         return @ptrCast(addr);
     }
 
+    if (builtin.zig_backend == .stage2_riscv64) {
+        const aligned_len = mem.alignForward(usize, n, mem.page_size);
+        const slice = posix.mmap(
+            null,
+            aligned_len,
+            posix.PROT.READ | posix.PROT.WRITE,
+            .{ .TYPE = .PRIVATE, .ANONYMOUS = true },
+            -1,
+            0,
+        ) catch return null;
+        assert(mem.isAligned(@intFromPtr(slice.ptr), mem.page_size));
+        return slice.ptr;
+    }
+
     const aligned_len = mem.alignForward(usize, n, mem.page_size);
     const hint = @atomicLoad(@TypeOf(std.heap.next_mmap_addr_hint), &std.heap.next_mmap_addr_hint, .unordered);
     const slice = posix.mmap(