Skip to content
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

Add LoggingAllocator prototype #2338

Merged
merged 1 commit into from
Jun 27, 2019
Merged
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
47 changes: 47 additions & 0 deletions std/heap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,53 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
};
}

pub const NoErrorOutStream = std.io.OutStream(error{});
Copy link
Member

Choose a reason for hiding this comment

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

How does one acquire a NoErrorOutStream? If I were to for example use (try std.io.getStdErr()).outStream() this outstream has errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've used this before....:

diff --git a/std/debug.zig b/std/debug.zig
index c5741f390..6425a7526 100644
--- a/std/debug.zig
+++ b/std/debug.zig
@@ -2339,7 +2339,15 @@ fn readILeb128(in_stream: var) !i64 {
 }
 
 /// This should only be used in temporary test programs.
-pub const global_allocator = &global_fixed_allocator.allocator;
+var NoErrorStdErrStream = std.heap.NoErrorOutStream {
+    .writeFn = struct {
+        pub fn writeFn(out_stream: *std.heap.NoErrorOutStream, bytes: []const u8) error{}!void {
+            const stderr = io.getStdErr() catch return;
+            stderr.write(bytes) catch return;
+        }
+    }.writeFn,
+};
+pub const global_allocator = &std.heap.LoggingAllocator.init(&global_fixed_allocator.allocator, &NoErrorStdErrStream).allocator;

pub const LoggingAllocator = struct {
Copy link
Member

Choose a reason for hiding this comment

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

doc comments please. when should someone use this? what are the semantics?

allocator: Allocator,
parentAllocator: *Allocator,
outStream: *NoErrorOutStream,

const Self = @This();

pub fn init(parentAllocator: *Allocator, outStream: *NoErrorOutStream) Self {
return Self{
.allocator = Allocator{
.reallocFn = realloc,
.shrinkFn = shrink,
},
.parentAllocator = parentAllocator,
.outStream = outStream,
};
}

fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
const self = @fieldParentPtr(Self, "allocator", allocator);
if (old_mem.len == 0) {
self.outStream.print("allocation of {} ", new_size) catch unreachable;
} else {
self.outStream.print("resize from {} to {} ", old_mem.len, new_size) catch unreachable;
}
const result = self.parentAllocator.reallocFn(self.parentAllocator, old_mem, old_align, new_size, new_align);
if (result) |buff| {
self.outStream.print("success!\n") catch unreachable;
} else |err| {
self.outStream.print("failure!\n") catch unreachable;
}
return result;
}

fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
const self = @fieldParentPtr(Self, "allocator", allocator);
const result = self.parentAllocator.shrinkFn(self.parentAllocator, old_mem, old_align, new_size, new_align);
if (new_size == 0) {
self.outStream.print("free of {} bytes success!\n", old_mem.len) catch unreachable;
} else {
self.outStream.print("shrink from {} bytes to {} bytes success!\n", old_mem.len, new_size) catch unreachable;
}
return result;
}
};

test "c_allocator" {
if (builtin.link_libc) {
var slice = try c_allocator.alloc(u8, 50);
Expand Down