From 4913de3c88d61637490bb450690d769b324508b5 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 29 Jan 2025 22:47:29 -0800 Subject: [PATCH] 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. --- lib/std/heap/general_purpose_allocator.zig | 6 +++--- lib/std/mem/Allocator.zig | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/std/heap/general_purpose_allocator.zig b/lib/std/heap/general_purpose_allocator.zig index c23f8dcd79..bf3f9df76d 100644 --- a/lib/std/heap/general_purpose_allocator.zig +++ b/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, diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index 7f3334f1d2..277758501b 100644 --- a/lib/std/mem/Allocator.zig +++ b/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