authorgravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2021-10-31 21:45:27+00:00
committergravatar for leecannon@leecannon.xyzLee Cannon <leecannon@leecannon.xyz> 2021-11-30 23:32:48+00:00
log02e5e0ba1fec38a3f8ed20e24219966f944a4ec2
tree76a893c32e501df5a5ad774566697f2b5b41d163
parent9377f32c089a925d7e6f1c64c1ce7777d108213c
signaturelock-open Commit is signed but in an unrecognized format.

allocgate: apply missed changes


3 files changed, 12 insertions(+), 10 deletions(-)

doc/langref.html.in+1-1
...@@ -10200,7 +10200,7 @@ test "string literal to constant slice" {...@@ -10200,7 +10200,7 @@ test "string literal to constant slice" {
10200 {#header_open|Implementing an Allocator#}10200 {#header_open|Implementing an Allocator#}
10201 <p>Zig programmers can implement their own allocators by fulfilling the Allocator interface.10201 <p>Zig programmers can implement their own allocators by fulfilling the Allocator interface.
10202 In order to do this one must read carefully the documentation comments in std/mem.zig and10202 In order to do this one must read carefully the documentation comments in std/mem.zig and
10203 then supply a {#syntax#}reallocFn{#endsyntax#} and a {#syntax#}shrinkFn{#endsyntax#}.10203 then supply a {#syntax#}allocFn{#endsyntax#} and a {#syntax#}resizeFn{#endsyntax#}.
10204 </p>10204 </p>
10205 <p>10205 <p>
10206 There are many example allocators to look at for inspiration. Look at std/heap.zig and10206 There are many example allocators to look at for inspiration. Look at std/heap.zig and
lib/std/heap/general_purpose_allocator.zig+8-6
...@@ -555,7 +555,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -555,7 +555,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
555555
556 // Do memory limit accounting with requested sizes rather than what backing_allocator returns556 // Do memory limit accounting with requested sizes rather than what backing_allocator returns
557 // because if we want to return error.OutOfMemory, we have to leave allocation untouched, and557 // because if we want to return error.OutOfMemory, we have to leave allocation untouched, and
558 // that is impossible to guarantee after calling backing_allocator.resizeFn.558 // that is impossible to guarantee after calling backing_allocator.vtable.resize.
559 const prev_req_bytes = self.total_requested_bytes;559 const prev_req_bytes = self.total_requested_bytes;
560 if (config.enable_memory_limit) {560 if (config.enable_memory_limit) {
561 const new_req_bytes = prev_req_bytes + new_size - entry.value_ptr.requested_size;561 const new_req_bytes = prev_req_bytes + new_size - entry.value_ptr.requested_size;
...@@ -571,7 +571,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -571,7 +571,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
571 const result_len = if (config.never_unmap and new_size == 0)571 const result_len = if (config.never_unmap and new_size == 0)
572 0572 0
573 else573 else
574 try self.backing_allocator.resizeFn(self.backing_allocator.ptr, old_mem, old_align, new_size, len_align, ret_addr);574 try self.backing_allocator.vtable.resize(self.backing_allocator.ptr, old_mem, old_align, new_size, len_align, ret_addr);
575575
576 if (config.enable_memory_limit) {576 if (config.enable_memory_limit) {
577 entry.value_ptr.requested_size = new_size;577 entry.value_ptr.requested_size = new_size;
...@@ -764,7 +764,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {...@@ -764,7 +764,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
764 const new_aligned_size = math.max(len, ptr_align);764 const new_aligned_size = math.max(len, ptr_align);
765 if (new_aligned_size > largest_bucket_object_size) {765 if (new_aligned_size > largest_bucket_object_size) {
766 try self.large_allocations.ensureUnusedCapacity(self.backing_allocator, 1);766 try self.large_allocations.ensureUnusedCapacity(self.backing_allocator, 1);
767 const slice = try self.backing_allocator.allocFn(self.backing_allocator.ptr, len, ptr_align, len_align, ret_addr);767 const slice = try self.backing_allocator.vtable.alloc(self.backing_allocator.ptr, len, ptr_align, len_align, ret_addr);
768768
769 const gop = self.large_allocations.getOrPutAssumeCapacity(@ptrToInt(slice.ptr));769 const gop = self.large_allocations.getOrPutAssumeCapacity(@ptrToInt(slice.ptr));
770 if (config.retain_metadata and !config.never_unmap) {770 if (config.retain_metadata and !config.never_unmap) {
...@@ -1191,10 +1191,12 @@ test "double frees" {...@@ -1191,10 +1191,12 @@ test "double frees" {
1191test "bug 9995 fix, large allocs count requested size not backing size" {1191test "bug 9995 fix, large allocs count requested size not backing size" {
1192 // with AtLeast, buffer likely to be larger than requested, especially when shrinking1192 // with AtLeast, buffer likely to be larger than requested, especially when shrinking
1193 var gpa = GeneralPurposeAllocator(.{ .enable_memory_limit = true }){};1193 var gpa = GeneralPurposeAllocator(.{ .enable_memory_limit = true }){};
1194 var buf = try gpa.allocator.allocAdvanced(u8, 1, page_size + 1, .at_least);1194 const allocator = gpa.allocator();
1195
1196 var buf = try allocator.allocAdvanced(u8, 1, page_size + 1, .at_least);
1195 try std.testing.expect(gpa.total_requested_bytes == page_size + 1);1197 try std.testing.expect(gpa.total_requested_bytes == page_size + 1);
1196 buf = try gpa.allocator.reallocAtLeast(buf, 1);1198 buf = try allocator.reallocAtLeast(buf, 1);
1197 try std.testing.expect(gpa.total_requested_bytes == 1);1199 try std.testing.expect(gpa.total_requested_bytes == 1);
1198 buf = try gpa.allocator.reallocAtLeast(buf, 2);1200 buf = try allocator.reallocAtLeast(buf, 2);
1199 try std.testing.expect(gpa.total_requested_bytes == 2);1201 try std.testing.expect(gpa.total_requested_bytes == 2);
1200}1202}
lib/std/mem/Allocator.zig+3-3
...@@ -108,7 +108,7 @@ pub fn NoResize(comptime AllocatorType: type) type {...@@ -108,7 +108,7 @@ pub fn NoResize(comptime AllocatorType: type) type {
108/// When the size/alignment is less than or equal to the previous allocation,108/// When the size/alignment is less than or equal to the previous allocation,
109/// this function returns `error.OutOfMemory` when the allocator decides the client109/// this function returns `error.OutOfMemory` when the allocator decides the client
110/// would be better off keeping the extra alignment/size. Clients will call110/// would be better off keeping the extra alignment/size. Clients will call
111/// `resizeFn` when they require the allocator to track a new alignment/size,111/// `vtable.resize` when they require the allocator to track a new alignment/size,
112/// and so this function should only return success when the allocator considers112/// and so this function should only return success when the allocator considers
113/// the reallocation desirable from the allocator's perspective.113/// the reallocation desirable from the allocator's perspective.
114/// As an example, `std.ArrayList` tracks a "capacity", and therefore can handle114/// As an example, `std.ArrayList` tracks a "capacity", and therefore can handle
...@@ -124,7 +124,7 @@ pub fn NoResize(comptime AllocatorType: type) type {...@@ -124,7 +124,7 @@ pub fn NoResize(comptime AllocatorType: type) type {
124fn reallocBytes(124fn reallocBytes(
125 self: Allocator,125 self: Allocator,
126 /// Guaranteed to be the same as what was returned from most recent call to126 /// Guaranteed to be the same as what was returned from most recent call to
127 /// `allocFn` or `resizeFn`.127 /// `vtable.alloc` or `vtable.resize`.
128 /// If `old_mem.len == 0` then this is a new allocation and `new_byte_count`128 /// If `old_mem.len == 0` then this is a new allocation and `new_byte_count`
129 /// is guaranteed to be >= 1.129 /// is guaranteed to be >= 1.
130 old_mem: []u8,130 old_mem: []u8,
...@@ -507,7 +507,7 @@ pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) ![:0]T {...@@ -507,7 +507,7 @@ pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) ![:0]T {
507 return new_buf[0..m.len :0];507 return new_buf[0..m.len :0];
508}508}
509509
510/// Call `resizeFn`, but caller guarantees that `new_len` <= `buf.len` meaning510/// Call `vtable.resize`, but caller guarantees that `new_len` <= `buf.len` meaning
511/// error.OutOfMemory should be impossible.511/// error.OutOfMemory should be impossible.
512/// This function allows a runtime `buf_align` value. Callers should generally prefer512/// This function allows a runtime `buf_align` value. Callers should generally prefer
513/// to call `shrink` directly.513/// to call `shrink` directly.