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