@@ -49,6 +49,7 @@ pub const Allocator = struct {
49
49
pub fn destroy (self : * Allocator , ptr : var ) void {
50
50
const non_const_ptr = @intToPtr ([* ]u8 , @ptrToInt (ptr ));
51
51
self .freeFn (self , non_const_ptr [0.. @sizeOf (@typeOf (ptr ).Child )]);
52
+ _ = std .valgrind .freeLikeBlock (non_const_ptr , 0 );
52
53
}
53
54
54
55
pub fn alloc (self : * Allocator , comptime T : type , n : usize ) ! []T {
@@ -62,6 +63,7 @@ pub const Allocator = struct {
62
63
const byte_count = math .mul (usize , @sizeOf (T ), n ) catch return Error .OutOfMemory ;
63
64
const byte_slice = try self .allocFn (self , byte_count , alignment );
64
65
assert (byte_slice .len == byte_count );
66
+ _ = std .valgrind .mallocLikeBlock (byte_slice , 0 , false );
65
67
// This loop gets optimized out in ReleaseFast mode
66
68
for (byte_slice ) | * byte | {
67
69
byte .* = undefined ;
@@ -86,6 +88,12 @@ pub const Allocator = struct {
86
88
const byte_count = math .mul (usize , @sizeOf (T ), n ) catch return Error .OutOfMemory ;
87
89
const byte_slice = try self .reallocFn (self , old_byte_slice , byte_count , alignment );
88
90
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
+ }
89
97
if (n > old_mem .len ) {
90
98
// This loop gets optimized out in ReleaseFast mode
91
99
for (byte_slice [old_byte_slice .len .. ]) | * byte | {
@@ -114,8 +122,15 @@ pub const Allocator = struct {
114
122
// n <= old_mem.len and the multiplication didn't overflow for that operation.
115
123
const byte_count = @sizeOf (T ) * n ;
116
124
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 ;
118
127
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
+ }
119
134
return @bytesToSlice (T , @alignCast (alignment , byte_slice ));
120
135
}
121
136
@@ -124,6 +139,7 @@ pub const Allocator = struct {
124
139
if (bytes .len == 0 ) return ;
125
140
const non_const_ptr = @intToPtr ([* ]u8 , @ptrToInt (bytes .ptr ));
126
141
self .freeFn (self , non_const_ptr [0.. bytes .len ]);
142
+ _ = std .valgrind .freeLikeBlock (non_const_ptr , 0 );
127
143
}
128
144
};
129
145
0 commit comments