Skip to content

Commit

Permalink
FIX resize() for non u8 element types. (#9806)
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanbroad committed Sep 22, 2021
1 parent 4afe4bd commit e14fcd6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 40 additions & 0 deletions lib/std/mem.zig
Expand Up @@ -147,6 +147,46 @@ test "mem.Allocator basics" {
try testing.expectError(error.OutOfMemory, failAllocator.allocSentinel(u8, 1, 0));
}

test "Allocator.resize" {
const primitiveIntTypes = .{
i8,
u8,
i16,
u16,
i32,
u32,
i64,
u64,
i128,
u128,
isize,
usize,
};
inline for (primitiveIntTypes) |T| {
var values = try testing.allocator.alloc(T, 100);
defer testing.allocator.free(values);

for (values) |*v, i| v.* = @intCast(T, i);
values = try testing.allocator.resize(values, values.len + 10);
try testing.expect(values.len == 110);
}

const primitiveFloatTypes = .{
f16,
f32,
f64,
f128,
};
inline for (primitiveFloatTypes) |T| {
var values = try testing.allocator.alloc(T, 100);
defer testing.allocator.free(values);

for (values) |*v, i| v.* = @intToFloat(T, i);
values = try testing.allocator.resize(values, values.len + 10);
try testing.expect(values.len == 110);
}
}

/// Copy all of source into dest at position 0.
/// dest.len must be >= source.len.
/// If the slices overlap, dest.ptr must be <= src.ptr.
Expand Down
2 changes: 1 addition & 1 deletion lib/std/mem/Allocator.zig
Expand Up @@ -313,7 +313,7 @@ pub fn resize(self: *Allocator, old_mem: anytype, new_n: usize) Error!@TypeOf(ol
const new_byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory;
const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @returnAddress());
assert(rc == new_byte_count);
const new_byte_slice = old_mem.ptr[0..new_byte_count];
const new_byte_slice = old_byte_slice.ptr[0..new_byte_count];
return mem.bytesAsSlice(T, new_byte_slice);
}

Expand Down

0 comments on commit e14fcd6

Please sign in to comment.