authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-10 15:38:05-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-12-10 15:38:05-05:00
log22dc713a2f6b19ccba31434cfa2bc619c1a2c170
tree5ee07674adefe894491d944b53ce3ea7e2d33ea7
parent990db3c35a798a6e51dbbbc1696f3fe78c7a0faf

mem.Allocator initializes bytes to undefined


1 files changed, 13 insertions(+), 1 deletions(-)

std/mem.zig+13-1
......@@ -8,6 +8,7 @@ pub const Cmp = math.Cmp;
88pub const Allocator = struct {
99 /// Allocate byte_count bytes and return them in a slice, with the
1010 /// slice's pointer aligned at least to alignment bytes.
11 /// The returned newly allocated memory is undefined.
1112 allocFn: fn (self: &Allocator, byte_count: usize, alignment: u29) -> %[]u8,
1213
1314 /// If `new_byte_count > old_mem.len`:
......@@ -17,6 +18,8 @@ pub const Allocator = struct {
1718 /// If `new_byte_count <= old_mem.len`:
1819 /// * this function must return successfully.
1920 /// * alignment <= alignment of old_mem.ptr
21 ///
22 /// The returned newly allocated memory is undefined.
2023 reallocFn: fn (self: &Allocator, old_mem: []u8, new_byte_count: usize, alignment: u29) -> %[]u8,
2124
2225 /// Guaranteed: `old_mem.len` is the same as what was returned from `allocFn` or `reallocFn`
......@@ -40,6 +43,10 @@ pub const Allocator = struct {
4043 {
4144 const byte_count = %return math.mul(usize, @sizeOf(T), n);
4245 const byte_slice = %return self.allocFn(self, byte_count, alignment);
46 // This loop should get optimized out in ReleaseFast mode
47 for (byte_slice) |*byte| {
48 *byte = undefined;
49 }
4350 return ([]align(alignment) T)(@alignCast(alignment, byte_slice));
4451 }
4552
......@@ -54,8 +61,13 @@ pub const Allocator = struct {
5461 return self.alloc(T, n);
5562 }
5663
64 const old_byte_slice = ([]u8)(old_mem);
5765 const byte_count = %return math.mul(usize, @sizeOf(T), n);
58 const byte_slice = %return self.reallocFn(self, ([]u8)(old_mem), byte_count, alignment);
66 const byte_slice = %return self.reallocFn(self, old_byte_slice, byte_count, alignment);
67 // This loop should get optimized out in ReleaseFast mode
68 for (byte_slice[old_byte_slice.len..]) |*byte| {
69 *byte = undefined;
70 }
5971 return ([]T)(@alignCast(alignment, byte_slice));
6072 }
6173