authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-27 12:05:12-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-27 12:05:12-04:00
log6cd3995754adcdcb1873c3fbfc0ec98ba95b68b5
tree386bc97a4f257fee93e467605e8d89f4a349a6ef
parent516b5e649fa5f6c4043275b3dbe8e38273d79344
parent0041e00a7821f68c34f392b858ad1595c910dabc
signaturelock-open Commit is signed but in an unrecognized format.

Merge branch 'daurnimator-logging-allocator'


3 files changed, 56 insertions(+), 0 deletions(-)

CMakeLists.txt+1
...@@ -524,6 +524,7 @@ set(ZIG_STD_FILES...@@ -524,6 +524,7 @@ set(ZIG_STD_FILES
524 "hash/siphash.zig"524 "hash/siphash.zig"
525 "hash_map.zig"525 "hash_map.zig"
526 "heap.zig"526 "heap.zig"
527 "heap/logging_allocator.zig"
527 "io.zig"528 "io.zig"
528 "io/c_out_stream.zig"529 "io/c_out_stream.zig"
529 "io/seekable_stream.zig"530 "io/seekable_stream.zig"
std/heap.zig+2
...@@ -8,6 +8,8 @@ const builtin = @import("builtin");...@@ -8,6 +8,8 @@ const builtin = @import("builtin");
8const c = std.c;8const c = std.c;
9const maxInt = std.math.maxInt;9const maxInt = std.math.maxInt;
1010
11pub const LoggingAllocator = @import("heap/logging_allocator.zig").LoggingAllocator;
12
11const Allocator = mem.Allocator;13const Allocator = mem.Allocator;
1214
13pub const c_allocator = &c_allocator_state;15pub const c_allocator = &c_allocator_state;
std/heap/logging_allocator.zig created+53
...@@ -0,0 +1,53 @@
1const std = @import("../std.zig");
2const Allocator = std.mem.Allocator;
3
4const AnyErrorOutStream = std.io.OutStream(anyerror);
5
6/// This allocator is used in front of another allocator and logs to the provided stream
7/// on every call to the allocator. Stream errors are ignored.
8/// If https://github.com/ziglang/zig/issues/2586 is implemented, this API can be improved.
9pub const LoggingAllocator = struct {
10 allocator: Allocator,
11 parent_allocator: *Allocator,
12 out_stream: *AnyErrorOutStream,
13
14 const Self = @This();
15
16 pub fn init(parent_allocator: *Allocator, out_stream: *AnyErrorOutStream) Self {
17 return Self{
18 .allocator = Allocator{
19 .reallocFn = realloc,
20 .shrinkFn = shrink,
21 },
22 .parent_allocator = parent_allocator,
23 .out_stream = out_stream,
24 };
25 }
26
27 fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 {
28 const self = @fieldParentPtr(Self, "allocator", allocator);
29 if (old_mem.len == 0) {
30 self.out_stream.print("allocation of {} ", new_size) catch {};
31 } else {
32 self.out_stream.print("resize from {} to {} ", old_mem.len, new_size) catch {};
33 }
34 const result = self.parent_allocator.reallocFn(self.parent_allocator, old_mem, old_align, new_size, new_align);
35 if (result) |buff| {
36 self.out_stream.print("success!\n") catch {};
37 } else |err| {
38 self.out_stream.print("failure!\n") catch {};
39 }
40 return result;
41 }
42
43 fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 {
44 const self = @fieldParentPtr(Self, "allocator", allocator);
45 const result = self.parent_allocator.shrinkFn(self.parent_allocator, old_mem, old_align, new_size, new_align);
46 if (new_size == 0) {
47 self.out_stream.print("free of {} bytes success!\n", old_mem.len) catch {};
48 } else {
49 self.out_stream.print("shrink from {} bytes to {} bytes success!\n", old_mem.len, new_size) catch {};
50 }
51 return result;
52 }
53};