diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-08-11 06:19:10 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-11 06:19:10 -0400 |
| commit | 44a6172edbf6c99f4ebfeabb530756ed2a40c3c2 (patch) | |
| tree | fb158886b71bc3e1a19d09aea13b4e3d19b164ea /lib/std/array_list.zig | |
| parent | 9f1f60fd4311b37493a5297ccaac167d0270f521 (diff) | |
| parent | fa620ef710bbbf7f642fd1d6d24a188d02902988 (diff) | |
| download | zig-44a6172edbf6c99f4ebfeabb530756ed2a40c3c2.tar.gz zig-44a6172edbf6c99f4ebfeabb530756ed2a40c3c2.zip | |
Merge pull request #12405 from ziglang/macho-aligned-ptrs
link.MachO: use accurate alignment attribute on pointers
Diffstat (limited to 'lib/std/array_list.zig')
| -rw-r--r-- | lib/std/array_list.zig | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 655bdeaa42..a8c53c3142 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -221,6 +221,30 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type { mem.copy(T, self.items[old_len..], items); } + /// Append an unaligned slice of items to the list. Allocates more + /// memory as necessary. Only call this function if calling + /// `appendSlice` instead would be a compile error. + pub fn appendUnalignedSlice(self: *Self, items: []align(1) const T) Allocator.Error!void { + try self.ensureUnusedCapacity(items.len); + self.appendUnalignedSliceAssumeCapacity(items); + } + + /// Append the slice of items to the list, asserting the capacity is already + /// enough to store the new items. **Does not** invalidate pointers. + /// Only call this function if calling `appendSliceAssumeCapacity` instead + /// would be a compile error. + pub fn appendUnalignedSliceAssumeCapacity(self: *Self, items: []align(1) const T) void { + const old_len = self.items.len; + const new_len = old_len + items.len; + assert(new_len <= self.capacity); + self.items.len = new_len; + @memcpy( + @ptrCast([*]align(@alignOf(T)) u8, self.items.ptr + old_len), + @ptrCast([*]const u8, items.ptr), + items.len * @sizeOf(T), + ); + } + pub const Writer = if (T != u8) @compileError("The Writer interface is only defined for ArrayList(u8) " ++ "but the given type is ArrayList(" ++ @typeName(T) ++ ")") @@ -592,6 +616,29 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ mem.copy(T, self.items[old_len..], items); } + /// Append the slice of items to the list. Allocates more + /// memory as necessary. Only call this function if a call to `appendSlice` instead would + /// be a compile error. + pub fn appendUnalignedSlice(self: *Self, allocator: Allocator, items: []align(1) const T) Allocator.Error!void { + try self.ensureUnusedCapacity(allocator, items.len); + self.appendUnalignedSliceAssumeCapacity(items); + } + + /// Append an unaligned slice of items to the list, asserting the capacity is enough + /// to store the new items. Only call this function if a call to `appendSliceAssumeCapacity` + /// instead would be a compile error. + pub fn appendUnalignedSliceAssumeCapacity(self: *Self, items: []align(1) const T) void { + const old_len = self.items.len; + const new_len = old_len + items.len; + assert(new_len <= self.capacity); + self.items.len = new_len; + @memcpy( + @ptrCast([*]align(@alignOf(T)) u8, self.items.ptr + old_len), + @ptrCast([*]const u8, items.ptr), + items.len * @sizeOf(T), + ); + } + pub const WriterContext = struct { self: *Self, allocator: Allocator, @@ -899,6 +946,14 @@ test "std.ArrayList/ArrayListUnmanaged.basic" { try testing.expect(list.pop() == 1); try testing.expect(list.items.len == 9); + var unaligned: [3]i32 align(1) = [_]i32{ 4, 5, 6 }; + list.appendUnalignedSlice(&unaligned) catch unreachable; + try testing.expect(list.items.len == 12); + try testing.expect(list.pop() == 6); + try testing.expect(list.pop() == 5); + try testing.expect(list.pop() == 4); + try testing.expect(list.items.len == 9); + list.appendSlice(&[_]i32{}) catch unreachable; try testing.expect(list.items.len == 9); @@ -941,6 +996,14 @@ test "std.ArrayList/ArrayListUnmanaged.basic" { try testing.expect(list.pop() == 1); try testing.expect(list.items.len == 9); + var unaligned: [3]i32 align(1) = [_]i32{ 4, 5, 6 }; + list.appendUnalignedSlice(a, &unaligned) catch unreachable; + try testing.expect(list.items.len == 12); + try testing.expect(list.pop() == 6); + try testing.expect(list.pop() == 5); + try testing.expect(list.pop() == 4); + try testing.expect(list.items.len == 9); + list.appendSlice(a, &[_]i32{}) catch unreachable; try testing.expect(list.items.len == 9); |
