Skip to content

New allocator corrections #5734

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/std/heap/arena_allocator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub const ArenaAllocator = struct {
const actual_min_size = minimum_size + (@sizeOf(BufNode) + 16);
const big_enough_len = prev_len + actual_min_size;
const len = big_enough_len + big_enough_len / 2;
const buf = try self.child_allocator.alignedAlloc(u8, @alignOf(BufNode), len);
const buf = try self.child_allocator.allocAdvanced(u8, @alignOf(BufNode), len, .at_least);
const buf_node_slice = mem.bytesAsSlice(BufNode, buf[0..@sizeOf(BufNode)]);
const buf_node = &buf_node_slice[0];
buf_node.* = BufNode{
Expand Down
36 changes: 18 additions & 18 deletions lib/std/heap/logging_allocator.zig
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
const std = @import("../std.zig");
const Allocator = std.mem.Allocator;

/// This allocator is used in front of another allocator and logs to the provided stream
/// on every call to the allocator. Stream errors are ignored.
/// This allocator is used in front of another allocator and logs to the provided writer
/// on every call to the allocator. Writer errors are ignored.
/// If https://github.com/ziglang/zig/issues/2586 is implemented, this API can be improved.
pub fn LoggingAllocator(comptime OutStreamType: type) type {
pub fn LoggingAllocator(comptime WriterType: type) type {
Comment on lines +4 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be changed to use std.log facilities, and it no longer needs to be a generic type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that mean we won't be able to test the logging output anymore?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can add a "compare output" test case for it: https://github.com/ziglang/zig/blob/master/test/compare_output.zig

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gave me another idea: #5738

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrewrk

This should be changed to use std.log facilities

What to use?
log.notice, log.info, log.debug?

What scope? .loggingAllocator?

we can add a "compare output" test case for it

How? With log function in compare_output.zig?

return struct {
allocator: Allocator,
parent_allocator: *Allocator,
out_stream: OutStreamType,
writer: WriterType,

const Self = @This();

pub fn init(parent_allocator: *Allocator, out_stream: OutStreamType) Self {
pub fn init(parent_allocator: *Allocator, writer: WriterType) Self {
return Self{
.allocator = Allocator{
.allocFn = alloc,
.resizeFn = resize,
},
.parent_allocator = parent_allocator,
.out_stream = out_stream,
.writer = writer,
};
}

fn alloc(allocator: *Allocator, len: usize, ptr_align: u29, len_align: u29) error{OutOfMemory}![]u8 {
const self = @fieldParentPtr(Self, "allocator", allocator);
self.out_stream.print("alloc : {}", .{len}) catch {};
self.writer.print("alloc : {}", .{len}) catch {};
const result = self.parent_allocator.callAllocFn(len, ptr_align, len_align);
if (result) |buff| {
self.out_stream.print(" success!\n", .{}) catch {};
self.writer.print(" success!\n", .{}) catch {};
} else |err| {
self.out_stream.print(" failure!\n", .{}) catch {};
self.writer.print(" failure!\n", .{}) catch {};
}
return result;
}

fn resize(allocator: *Allocator, buf: []u8, new_len: usize, len_align: u29) error{OutOfMemory}!usize {
const self = @fieldParentPtr(Self, "allocator", allocator);
if (new_len == 0) {
self.out_stream.print("free : {}\n", .{buf.len}) catch {};
self.writer.print("free : {}\n", .{buf.len}) catch {};
} else if (new_len <= buf.len) {
self.out_stream.print("shrink: {} to {}\n", .{buf.len, new_len}) catch {};
self.writer.print("shrink: {} to {}\n", .{buf.len, new_len}) catch {};
} else {
self.out_stream.print("expand: {} to {}", .{ buf.len, new_len }) catch {};
self.writer.print("expand: {} to {}", .{ buf.len, new_len }) catch {};
}
if (self.parent_allocator.callResizeFn(buf, new_len, len_align)) |resized_len| {
if (new_len > buf.len) {
self.out_stream.print(" success!\n", .{}) catch {};
self.writer.print(" success!\n", .{}) catch {};
}
return resized_len;
} else |e| {
std.debug.assert(new_len > buf.len);
self.out_stream.print(" failure!\n", .{}) catch {};
self.writer.print(" failure!\n", .{}) catch {};
return e;
}
}
Expand All @@ -60,9 +60,9 @@ pub fn LoggingAllocator(comptime OutStreamType: type) type {

pub fn loggingAllocator(
parent_allocator: *Allocator,
out_stream: var,
) LoggingAllocator(@TypeOf(out_stream)) {
return LoggingAllocator(@TypeOf(out_stream)).init(parent_allocator, out_stream);
writer: var,
) LoggingAllocator(@TypeOf(writer)) {
return LoggingAllocator(@TypeOf(writer)).init(parent_allocator, writer);
}

test "LoggingAllocator" {
Expand All @@ -71,7 +71,7 @@ test "LoggingAllocator" {

var allocator_buf: [10]u8 = undefined;
var fixedBufferAllocator = std.mem.validationWrap(std.heap.FixedBufferAllocator.init(&allocator_buf));
const allocator = &loggingAllocator(&fixedBufferAllocator.allocator, fbs.outStream()).allocator;
const allocator = &loggingAllocator(&fixedBufferAllocator.allocator, fbs.writer()).allocator;

var a = try allocator.alloc(u8, 10);
a.len = allocator.shrinkBytes(a, 5, 0);
Expand Down