mirror of
https://github.com/ziglang/zig.git
synced 2025-12-09 07:43:10 +00:00
fixups
* move LoggingAllocator to its own file
* style conventions
* add documentation
* use `anyerror` instead of `error{}` for the stream
This commit is contained in:
parent
c346b257bb
commit
0041e00a78
@ -524,6 +524,7 @@ set(ZIG_STD_FILES
|
|||||||
"hash/siphash.zig"
|
"hash/siphash.zig"
|
||||||
"hash_map.zig"
|
"hash_map.zig"
|
||||||
"heap.zig"
|
"heap.zig"
|
||||||
|
"heap/logging_allocator.zig"
|
||||||
"io.zig"
|
"io.zig"
|
||||||
"io/c_out_stream.zig"
|
"io/c_out_stream.zig"
|
||||||
"io/seekable_stream.zig"
|
"io/seekable_stream.zig"
|
||||||
|
|||||||
49
std/heap.zig
49
std/heap.zig
@ -8,6 +8,8 @@ const builtin = @import("builtin");
|
|||||||
const c = std.c;
|
const c = std.c;
|
||||||
const maxInt = std.math.maxInt;
|
const maxInt = std.math.maxInt;
|
||||||
|
|
||||||
|
pub const LoggingAllocator = @import("heap/logging_allocator.zig").LoggingAllocator;
|
||||||
|
|
||||||
const Allocator = mem.Allocator;
|
const Allocator = mem.Allocator;
|
||||||
|
|
||||||
pub const c_allocator = &c_allocator_state;
|
pub const c_allocator = &c_allocator_state;
|
||||||
@ -713,53 +715,6 @@ pub fn StackFallbackAllocator(comptime size: usize) type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const NoErrorOutStream = std.io.OutStream(error{});
|
|
||||||
pub const LoggingAllocator = struct {
|
|
||||||
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" {
|
test "c_allocator" {
|
||||||
if (builtin.link_libc) {
|
if (builtin.link_libc) {
|
||||||
var slice = try c_allocator.alloc(u8, 50);
|
var slice = try c_allocator.alloc(u8, 50);
|
||||||
|
|||||||
53
std/heap/logging_allocator.zig
Normal file
53
std/heap/logging_allocator.zig
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
const std = @import("../std.zig");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
|
||||||
|
const AnyErrorOutStream = std.io.OutStream(anyerror);
|
||||||
|
|
||||||
|
/// 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.
|
||||||
|
/// If https://github.com/ziglang/zig/issues/2586 is implemented, this API can be improved.
|
||||||
|
pub const LoggingAllocator = struct {
|
||||||
|
allocator: Allocator,
|
||||||
|
parent_allocator: *Allocator,
|
||||||
|
out_stream: *AnyErrorOutStream,
|
||||||
|
|
||||||
|
const Self = @This();
|
||||||
|
|
||||||
|
pub fn init(parent_allocator: *Allocator, out_stream: *AnyErrorOutStream) Self {
|
||||||
|
return Self{
|
||||||
|
.allocator = Allocator{
|
||||||
|
.reallocFn = realloc,
|
||||||
|
.shrinkFn = shrink,
|
||||||
|
},
|
||||||
|
.parent_allocator = parent_allocator,
|
||||||
|
.out_stream = out_stream,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
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.out_stream.print("allocation of {} ", new_size) catch {};
|
||||||
|
} else {
|
||||||
|
self.out_stream.print("resize from {} to {} ", old_mem.len, new_size) catch {};
|
||||||
|
}
|
||||||
|
const result = self.parent_allocator.reallocFn(self.parent_allocator, old_mem, old_align, new_size, new_align);
|
||||||
|
if (result) |buff| {
|
||||||
|
self.out_stream.print("success!\n") catch {};
|
||||||
|
} else |err| {
|
||||||
|
self.out_stream.print("failure!\n") catch {};
|
||||||
|
}
|
||||||
|
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.parent_allocator.shrinkFn(self.parent_allocator, old_mem, old_align, new_size, new_align);
|
||||||
|
if (new_size == 0) {
|
||||||
|
self.out_stream.print("free of {} bytes success!\n", old_mem.len) catch {};
|
||||||
|
} else {
|
||||||
|
self.out_stream.print("shrink from {} bytes to {} bytes success!\n", old_mem.len, new_size) catch {};
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user