Commit 4913de3c88

Andrew Kelley <andrew@ziglang.org>
2025-01-30 07:47:29
GeneralPurposeAllocator: minimal fix
This keeps the implementation matching master branch, however, introduces a compile error that applications can work around by explicitly setting page_size_max and page_size_min to match their computer's settings, in the case that those values are not already equal. I plan to rework this allocator in a follow-up enhancement with the goal of reducing total active memory mappings.
1 parent 95a0474
Changed files (2)
lib/std/heap/general_purpose_allocator.zig
@@ -99,7 +99,7 @@ const math = std.math;
 const assert = std.debug.assert;
 const mem = std.mem;
 const Allocator = std.mem.Allocator;
-const page_size = std.mem.page_size;
+const page_size = std.heap.pageSize(); // TODO: allow this to be runtime known
 const StackTrace = std.builtin.StackTrace;
 
 /// Integer type for pointing to slots in a small allocation
@@ -1040,8 +1040,8 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
 
             const bucket_size = bucketSize(size_class);
             const bucket_bytes = try self.backing_allocator.alignedAlloc(u8, @alignOf(BucketHeader), bucket_size);
-            const ptr = @as(*BucketHeader, @ptrCast(bucket_bytes.ptr));
-            ptr.* = BucketHeader{
+            const ptr: *BucketHeader = @ptrCast(bucket_bytes.ptr);
+            ptr.* = .{
                 .page = page.ptr,
                 .alloc_cursor = 0,
                 .used_count = 0,
lib/std/mem/Allocator.zig
@@ -227,7 +227,7 @@ fn allocBytesWithAlignment(self: Allocator, comptime alignment: u29, byte_count:
     const byte_ptr = self.rawAlloc(byte_count, log2a(alignment), return_address) orelse return Error.OutOfMemory;
     // TODO: https://github.com/ziglang/zig/issues/4298
     @memset(byte_ptr[0..byte_count], undefined);
-    return @as([*]align(alignment) u8, @alignCast(byte_ptr));
+    return @alignCast(byte_ptr);
 }
 
 /// Requests to modify the size of an allocation. It is guaranteed to not move