authorgravatar for coleman.broaddus@gmail.comColeman Broaddus <coleman.broaddus@gmail.com> 2021-09-22 05:09:16-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-09-22 12:09:16+03:00
loge14fcd60cb11d731ca19f97e5b0b6247aa7cf07b
tree8bf800c13de400f3c8492225a385e0f4552430f0
parent4afe4bdfe7dda638b35a9d388aac9a53d18cc4ba
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

FIX resize() for non u8 element types. (#9806)


2 files changed, 41 insertions(+), 1 deletions(-)

lib/std/mem.zig+40
...@@ -147,6 +147,46 @@ test "mem.Allocator basics" {...@@ -147,6 +147,46 @@ test "mem.Allocator basics" {
147 try testing.expectError(error.OutOfMemory, failAllocator.allocSentinel(u8, 1, 0));147 try testing.expectError(error.OutOfMemory, failAllocator.allocSentinel(u8, 1, 0));
148}148}
149149
150test "Allocator.resize" {
151 const primitiveIntTypes = .{
152 i8,
153 u8,
154 i16,
155 u16,
156 i32,
157 u32,
158 i64,
159 u64,
160 i128,
161 u128,
162 isize,
163 usize,
164 };
165 inline for (primitiveIntTypes) |T| {
166 var values = try testing.allocator.alloc(T, 100);
167 defer testing.allocator.free(values);
168
169 for (values) |*v, i| v.* = @intCast(T, i);
170 values = try testing.allocator.resize(values, values.len + 10);
171 try testing.expect(values.len == 110);
172 }
173
174 const primitiveFloatTypes = .{
175 f16,
176 f32,
177 f64,
178 f128,
179 };
180 inline for (primitiveFloatTypes) |T| {
181 var values = try testing.allocator.alloc(T, 100);
182 defer testing.allocator.free(values);
183
184 for (values) |*v, i| v.* = @intToFloat(T, i);
185 values = try testing.allocator.resize(values, values.len + 10);
186 try testing.expect(values.len == 110);
187 }
188}
189
150/// Copy all of source into dest at position 0.190/// Copy all of source into dest at position 0.
151/// dest.len must be >= source.len.191/// dest.len must be >= source.len.
152/// If the slices overlap, dest.ptr must be <= src.ptr.192/// If the slices overlap, dest.ptr must be <= src.ptr.
lib/std/mem/Allocator.zig+1-1
...@@ -313,7 +313,7 @@ pub fn resize(self: *Allocator, old_mem: anytype, new_n: usize) Error!@TypeOf(ol...@@ -313,7 +313,7 @@ pub fn resize(self: *Allocator, old_mem: anytype, new_n: usize) Error!@TypeOf(ol
313 const new_byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory;313 const new_byte_count = math.mul(usize, @sizeOf(T), new_n) catch return Error.OutOfMemory;
314 const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @returnAddress());314 const rc = try self.resizeFn(self, old_byte_slice, Slice.alignment, new_byte_count, 0, @returnAddress());
315 assert(rc == new_byte_count);315 assert(rc == new_byte_count);
316 const new_byte_slice = old_mem.ptr[0..new_byte_count];316 const new_byte_slice = old_byte_slice.ptr[0..new_byte_count];
317 return mem.bytesAsSlice(T, new_byte_slice);317 return mem.bytesAsSlice(T, new_byte_slice);
318}318}
319319