Skip to content

Commit 7bbccfc

Browse files
committed
Use valgrind marks from Allocator
1 parent d68ae35 commit 7bbccfc

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

std/mem.zig

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub const Allocator = struct {
4949
pub fn destroy(self: *Allocator, ptr: var) void {
5050
const non_const_ptr = @intToPtr([*]u8, @ptrToInt(ptr));
5151
self.freeFn(self, non_const_ptr[0..@sizeOf(@typeOf(ptr).Child)]);
52+
_ = std.valgrind.freeLikeBlock(non_const_ptr, 0);
5253
}
5354

5455
pub fn alloc(self: *Allocator, comptime T: type, n: usize) ![]T {
@@ -62,6 +63,7 @@ pub const Allocator = struct {
6263
const byte_count = math.mul(usize, @sizeOf(T), n) catch return Error.OutOfMemory;
6364
const byte_slice = try self.allocFn(self, byte_count, alignment);
6465
assert(byte_slice.len == byte_count);
66+
_ = std.valgrind.mallocLikeBlock(byte_slice, 0, false);
6567
// This loop gets optimized out in ReleaseFast mode
6668
for (byte_slice) |*byte| {
6769
byte.* = undefined;
@@ -86,6 +88,12 @@ pub const Allocator = struct {
8688
const byte_count = math.mul(usize, @sizeOf(T), n) catch return Error.OutOfMemory;
8789
const byte_slice = try self.reallocFn(self, old_byte_slice, byte_count, alignment);
8890
assert(byte_slice.len == byte_count);
91+
if (byte_slice.ptr == old_byte_slice.ptr) {
92+
_ = std.valgrind.resizeInPlaceBlock(old_byte_slice, byte_count, 0);
93+
} else {
94+
_ = std.valgrind.freeLikeBlock(old_byte_slice.ptr, 0);
95+
_ = std.valgrind.mallocLikeBlock(byte_slice, 0, false);
96+
}
8997
if (n > old_mem.len) {
9098
// This loop gets optimized out in ReleaseFast mode
9199
for (byte_slice[old_byte_slice.len..]) |*byte| {
@@ -114,8 +122,15 @@ pub const Allocator = struct {
114122
// n <= old_mem.len and the multiplication didn't overflow for that operation.
115123
const byte_count = @sizeOf(T) * n;
116124

117-
const byte_slice = self.reallocFn(self, @sliceToBytes(old_mem), byte_count, alignment) catch unreachable;
125+
const old_byte_slice = @sliceToBytes(old_mem);
126+
const byte_slice = self.reallocFn(self, old_byte_slice, byte_count, alignment) catch unreachable;
118127
assert(byte_slice.len == byte_count);
128+
if (byte_slice.ptr == old_byte_slice.ptr) {
129+
_ = std.valgrind.resizeInPlaceBlock(old_byte_slice, byte_count, 0);
130+
} else {
131+
_ = std.valgrind.freeLikeBlock(old_byte_slice.ptr, 0);
132+
_ = std.valgrind.mallocLikeBlock(byte_slice, 0, false);
133+
}
119134
return @bytesToSlice(T, @alignCast(alignment, byte_slice));
120135
}
121136

@@ -124,6 +139,7 @@ pub const Allocator = struct {
124139
if (bytes.len == 0) return;
125140
const non_const_ptr = @intToPtr([*]u8, @ptrToInt(bytes.ptr));
126141
self.freeFn(self, non_const_ptr[0..bytes.len]);
142+
_ = std.valgrind.freeLikeBlock(non_const_ptr, 0);
127143
}
128144
};
129145

0 commit comments

Comments
 (0)