authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-29 22:47:29-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-02-06 14:23:23-08:00
log4913de3c88d61637490bb450690d769b324508b5
tree9651e9f565b05db0bfe72ba12e7e5bff8f0060fd
parent95a0474dc6bf26a7a86bea4c93e06b3ae0cd37cc

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.

2 files changed, 4 insertions(+), 4 deletions(-)

lib/std/heap/general_purpose_allocator.zig+3-3
...@@ -99,7 +99,7 @@ const math = std.math;...@@ -99,7 +99,7 @@ const math = std.math;
99const assert = std.debug.assert;99const assert = std.debug.assert;
100const mem = std.mem;100const mem = std.mem;
101const Allocator = std.mem.Allocator;101const Allocator = std.mem.Allocator;
102const page_size = std.mem.page_size;102const page_size = std.heap.pageSize(); // TODO: allow this to be runtime known
103const StackTrace = std.builtin.StackTrace;103const StackTrace = std.builtin.StackTrace;
104104
105/// Integer type for pointing to slots in a small allocation105/// Integer type for pointing to slots in a small allocation
...@@ -1040,8 +1040,8 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -1040,8 +1040,8 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
10401040
1041 const bucket_size = bucketSize(size_class);1041 const bucket_size = bucketSize(size_class);
1042 const bucket_bytes = try self.backing_allocator.alignedAlloc(u8, @alignOf(BucketHeader), bucket_size);1042 const bucket_bytes = try self.backing_allocator.alignedAlloc(u8, @alignOf(BucketHeader), bucket_size);
1043 const ptr = @as(*BucketHeader, @ptrCast(bucket_bytes.ptr));1043 const ptr: *BucketHeader = @ptrCast(bucket_bytes.ptr);
1044 ptr.* = BucketHeader{1044 ptr.* = .{
1045 .page = page.ptr,1045 .page = page.ptr,
1046 .alloc_cursor = 0,1046 .alloc_cursor = 0,
1047 .used_count = 0,1047 .used_count = 0,
lib/std/mem/Allocator.zig+1-1
...@@ -227,7 +227,7 @@ fn allocBytesWithAlignment(self: Allocator, comptime alignment: u29, byte_count:...@@ -227,7 +227,7 @@ fn allocBytesWithAlignment(self: Allocator, comptime alignment: u29, byte_count:
227 const byte_ptr = self.rawAlloc(byte_count, log2a(alignment), return_address) orelse return Error.OutOfMemory;227 const byte_ptr = self.rawAlloc(byte_count, log2a(alignment), return_address) orelse return Error.OutOfMemory;
228 // TODO: https://github.com/ziglang/zig/issues/4298228 // TODO: https://github.com/ziglang/zig/issues/4298
229 @memset(byte_ptr[0..byte_count], undefined);229 @memset(byte_ptr[0..byte_count], undefined);
230 return @as([*]align(alignment) u8, @alignCast(byte_ptr));230 return @alignCast(byte_ptr);
231}231}
232232
233/// Requests to modify the size of an allocation. It is guaranteed to not move233/// Requests to modify the size of an allocation. It is guaranteed to not move