authorgravatar for remeh@remeh.frremeh <remeh@remeh.fr> 2025-03-06 14:18:24+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-09 07:39:20+01:00
log02f63fdee9b7b183611cd02e98e66bbdec8ed462
tree2c38848a8985864f7e653e5a0edb6aa233c9d4ce
parent1eb729b9b9a03f896e06001d9791e55753c60dfa

std/containers: improve consistency using gpa parameter name for allocator.


2 files changed, 67 insertions(+), 69 deletions(-)

lib/std/array_list.zig+65-67
......@@ -50,19 +50,19 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
5050 }
5151
5252 /// Deinitialize with `deinit` or use `toOwnedSlice`.
53 pub fn init(allocator: Allocator) Self {
53 pub fn init(gpa: Allocator) Self {
5454 return Self{
5555 .items = &[_]T{},
5656 .capacity = 0,
57 .allocator = allocator,
57 .allocator = gpa,
5858 };
5959 }
6060
6161 /// Initialize with capacity to hold `num` elements.
6262 /// The resulting capacity will equal `num` exactly.
6363 /// Deinitialize with `deinit` or use `toOwnedSlice`.
64 pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Self {
65 var self = Self.init(allocator);
64 pub fn initCapacity(gpa: Allocator, num: usize) Allocator.Error!Self {
65 var self = Self.init(gpa);
6666 try self.ensureTotalCapacityPrecise(num);
6767 return self;
6868 }
......@@ -75,24 +75,24 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
7575 }
7676
7777 /// ArrayList takes ownership of the passed in slice. The slice must have been
78 /// allocated with `allocator`.
78 /// allocated with `gpa`.
7979 /// Deinitialize with `deinit` or use `toOwnedSlice`.
80 pub fn fromOwnedSlice(allocator: Allocator, slice: Slice) Self {
80 pub fn fromOwnedSlice(gpa: Allocator, slice: Slice) Self {
8181 return Self{
8282 .items = slice,
8383 .capacity = slice.len,
84 .allocator = allocator,
84 .allocator = gpa,
8585 };
8686 }
8787
8888 /// ArrayList takes ownership of the passed in slice. The slice must have been
89 /// allocated with `allocator`.
89 /// allocated with `gpa`.
9090 /// Deinitialize with `deinit` or use `toOwnedSlice`.
91 pub fn fromOwnedSliceSentinel(allocator: Allocator, comptime sentinel: T, slice: [:sentinel]T) Self {
91 pub fn fromOwnedSliceSentinel(gpa: Allocator, comptime sentinel: T, slice: [:sentinel]T) Self {
9292 return Self{
9393 .items = slice,
9494 .capacity = slice.len + 1,
95 .allocator = allocator,
95 .allocator = gpa,
9696 };
9797 }
9898
......@@ -646,9 +646,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
646646 /// Initialize with capacity to hold `num` elements.
647647 /// The resulting capacity will equal `num` exactly.
648648 /// Deinitialize with `deinit` or use `toOwnedSlice`.
649 pub fn initCapacity(allocator: Allocator, num: usize) Allocator.Error!Self {
649 pub fn initCapacity(gpa: Allocator, num: usize) Allocator.Error!Self {
650650 var self = Self{};
651 try self.ensureTotalCapacityPrecise(allocator, num);
651 try self.ensureTotalCapacityPrecise(gpa, num);
652652 return self;
653653 }
654654
......@@ -664,19 +664,18 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
664664 }
665665
666666 /// Release all allocated memory.
667 pub fn deinit(self: *Self, allocator: Allocator) void {
668 allocator.free(self.allocatedSlice());
667 pub fn deinit(self: *Self, gpa: Allocator) void {
668 gpa.free(self.allocatedSlice());
669669 self.* = undefined;
670670 }
671671
672672 /// Convert this list into an analogous memory-managed one.
673673 /// The returned list has ownership of the underlying memory.
674 pub fn toManaged(self: *Self, allocator: Allocator) ArrayListAligned(T, alignment) {
675 return .{ .items = self.items, .capacity = self.capacity, .allocator = allocator };
674 pub fn toManaged(self: *Self, gpa: Allocator) ArrayListAligned(T, alignment) {
675 return .{ .items = self.items, .capacity = self.capacity, .allocator = gpa };
676676 }
677677
678 /// ArrayListUnmanaged takes ownership of the passed in slice. The slice must have been
679 /// allocated with `allocator`.
678 /// ArrayListUnmanaged takes ownership of the passed in slice.
680679 /// Deinitialize with `deinit` or use `toOwnedSlice`.
681680 pub fn fromOwnedSlice(slice: Slice) Self {
682681 return Self{
......@@ -685,8 +684,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
685684 };
686685 }
687686
688 /// ArrayListUnmanaged takes ownership of the passed in slice. The slice must have been
689 /// allocated with `allocator`.
687 /// ArrayListUnmanaged takes ownership of the passed in slice.
690688 /// Deinitialize with `deinit` or use `toOwnedSlice`.
691689 pub fn fromOwnedSliceSentinel(comptime sentinel: T, slice: [:sentinel]T) Self {
692690 return Self{
......@@ -697,31 +695,31 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
697695
698696 /// The caller owns the returned memory. Empties this ArrayList.
699697 /// Its capacity is cleared, making deinit() safe but unnecessary to call.
700 pub fn toOwnedSlice(self: *Self, allocator: Allocator) Allocator.Error!Slice {
698 pub fn toOwnedSlice(self: *Self, gpa: Allocator) Allocator.Error!Slice {
701699 const old_memory = self.allocatedSlice();
702 if (allocator.remap(old_memory, self.items.len)) |new_items| {
700 if (gpa.remap(old_memory, self.items.len)) |new_items| {
703701 self.* = .empty;
704702 return new_items;
705703 }
706704
707 const new_memory = try allocator.alignedAlloc(T, alignment, self.items.len);
705 const new_memory = try gpa.alignedAlloc(T, alignment, self.items.len);
708706 @memcpy(new_memory, self.items);
709 self.clearAndFree(allocator);
707 self.clearAndFree(gpa);
710708 return new_memory;
711709 }
712710
713711 /// The caller owns the returned memory. ArrayList becomes empty.
714 pub fn toOwnedSliceSentinel(self: *Self, allocator: Allocator, comptime sentinel: T) Allocator.Error!SentinelSlice(sentinel) {
712 pub fn toOwnedSliceSentinel(self: *Self, gpa: Allocator, comptime sentinel: T) Allocator.Error!SentinelSlice(sentinel) {
715713 // This addition can never overflow because `self.items` can never occupy the whole address space
716 try self.ensureTotalCapacityPrecise(allocator, self.items.len + 1);
714 try self.ensureTotalCapacityPrecise(gpa, self.items.len + 1);
717715 self.appendAssumeCapacity(sentinel);
718 const result = try self.toOwnedSlice(allocator);
716 const result = try self.toOwnedSlice(gpa);
719717 return result[0 .. result.len - 1 :sentinel];
720718 }
721719
722720 /// Creates a copy of this ArrayList.
723 pub fn clone(self: Self, allocator: Allocator) Allocator.Error!Self {
724 var cloned = try Self.initCapacity(allocator, self.capacity);
721 pub fn clone(self: Self, gpa: Allocator) Allocator.Error!Self {
722 var cloned = try Self.initCapacity(gpa, self.capacity);
725723 cloned.appendSliceAssumeCapacity(self.items);
726724 return cloned;
727725 }
......@@ -731,8 +729,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
731729 /// This operation is O(N).
732730 /// Invalidates element pointers if additional memory is needed.
733731 /// Asserts that the index is in bounds or equal to the length.
734 pub fn insert(self: *Self, allocator: Allocator, i: usize, item: T) Allocator.Error!void {
735 const dst = try self.addManyAt(allocator, i, 1);
732 pub fn insert(self: *Self, gpa: Allocator, i: usize, item: T) Allocator.Error!void {
733 const dst = try self.addManyAt(gpa, i, 1);
736734 dst[0] = item;
737735 }
738736
......@@ -759,11 +757,11 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
759757 /// Asserts that the index is in bounds or equal to the length.
760758 pub fn addManyAt(
761759 self: *Self,
762 allocator: Allocator,
760 gpa: Allocator,
763761 index: usize,
764762 count: usize,
765763 ) Allocator.Error![]T {
766 var managed = self.toManaged(allocator);
764 var managed = self.toManaged(gpa);
767765 defer self.* = managed.moveToUnmanaged();
768766 return managed.addManyAt(index, count);
769767 }
......@@ -795,12 +793,12 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
795793 /// Asserts that the index is in bounds or equal to the length.
796794 pub fn insertSlice(
797795 self: *Self,
798 allocator: Allocator,
796 gpa: Allocator,
799797 index: usize,
800798 items: []const T,
801799 ) Allocator.Error!void {
802800 const dst = try self.addManyAt(
803 allocator,
801 gpa,
804802 index,
805803 items.len,
806804 );
......@@ -812,7 +810,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
812810 /// Asserts that the range is in bounds.
813811 pub fn replaceRange(
814812 self: *Self,
815 allocator: Allocator,
813 gpa: Allocator,
816814 start: usize,
817815 len: usize,
818816 new_items: []const T,
......@@ -823,7 +821,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
823821 const first = new_items[0..range.len];
824822 const rest = new_items[range.len..];
825823 @memcpy(range[0..first.len], first);
826 try self.insertSlice(allocator, after_range, rest);
824 try self.insertSlice(gpa, after_range, rest);
827825 } else {
828826 self.replaceRangeAssumeCapacity(start, len, new_items);
829827 }
......@@ -859,8 +857,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
859857
860858 /// Extend the list by 1 element. Allocates more memory as necessary.
861859 /// Invalidates element pointers if additional memory is needed.
862 pub fn append(self: *Self, allocator: Allocator, item: T) Allocator.Error!void {
863 const new_item_ptr = try self.addOne(allocator);
860 pub fn append(self: *Self, gpa: Allocator, item: T) Allocator.Error!void {
861 const new_item_ptr = try self.addOne(gpa);
864862 new_item_ptr.* = item;
865863 }
866864
......@@ -899,8 +897,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
899897 /// Append the slice of items to the list. Allocates more
900898 /// memory as necessary.
901899 /// Invalidates element pointers if additional memory is needed.
902 pub fn appendSlice(self: *Self, allocator: Allocator, items: []const T) Allocator.Error!void {
903 try self.ensureUnusedCapacity(allocator, items.len);
900 pub fn appendSlice(self: *Self, gpa: Allocator, items: []const T) Allocator.Error!void {
901 try self.ensureUnusedCapacity(gpa, items.len);
904902 self.appendSliceAssumeCapacity(items);
905903 }
906904
......@@ -918,8 +916,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
918916 /// memory as necessary. Only call this function if a call to `appendSlice` instead would
919917 /// be a compile error.
920918 /// Invalidates element pointers if additional memory is needed.
921 pub fn appendUnalignedSlice(self: *Self, allocator: Allocator, items: []align(1) const T) Allocator.Error!void {
922 try self.ensureUnusedCapacity(allocator, items.len);
919 pub fn appendUnalignedSlice(self: *Self, gpa: Allocator, items: []align(1) const T) Allocator.Error!void {
920 try self.ensureUnusedCapacity(gpa, items.len);
923921 self.appendUnalignedSliceAssumeCapacity(items);
924922 }
925923
......@@ -947,8 +945,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
947945 std.io.Writer(WriterContext, Allocator.Error, appendWrite);
948946
949947 /// Initializes a Writer which will append to the list.
950 pub fn writer(self: *Self, allocator: Allocator) Writer {
951 return .{ .context = .{ .self = self, .allocator = allocator } };
948 pub fn writer(self: *Self, gpa: Allocator) Writer {
949 return .{ .context = .{ .self = self, .allocator = gpa } };
952950 }
953951
954952 /// Same as `append` except it returns the number of bytes written,
......@@ -983,9 +981,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
983981 /// Invalidates element pointers if additional memory is needed.
984982 /// The function is inline so that a comptime-known `value` parameter will
985983 /// have a more optimal memset codegen in case it has a repeated byte pattern.
986 pub inline fn appendNTimes(self: *Self, allocator: Allocator, value: T, n: usize) Allocator.Error!void {
984 pub inline fn appendNTimes(self: *Self, gpa: Allocator, value: T, n: usize) Allocator.Error!void {
987985 const old_len = self.items.len;
988 try self.resize(allocator, try addOrOom(old_len, n));
986 try self.resize(gpa, try addOrOom(old_len, n));
989987 @memset(self.items[old_len..self.items.len], value);
990988 }
991989
......@@ -1004,15 +1002,15 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
10041002 /// Adjust the list length to `new_len`.
10051003 /// Additional elements contain the value `undefined`.
10061004 /// Invalidates element pointers if additional memory is needed.
1007 pub fn resize(self: *Self, allocator: Allocator, new_len: usize) Allocator.Error!void {
1008 try self.ensureTotalCapacity(allocator, new_len);
1005 pub fn resize(self: *Self, gpa: Allocator, new_len: usize) Allocator.Error!void {
1006 try self.ensureTotalCapacity(gpa, new_len);
10091007 self.items.len = new_len;
10101008 }
10111009
10121010 /// Reduce allocated capacity to `new_len`.
10131011 /// May invalidate element pointers.
10141012 /// Asserts that the new length is less than or equal to the previous length.
1015 pub fn shrinkAndFree(self: *Self, allocator: Allocator, new_len: usize) void {
1013 pub fn shrinkAndFree(self: *Self, gpa: Allocator, new_len: usize) void {
10161014 assert(new_len <= self.items.len);
10171015
10181016 if (@sizeOf(T) == 0) {
......@@ -1021,13 +1019,13 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
10211019 }
10221020
10231021 const old_memory = self.allocatedSlice();
1024 if (allocator.remap(old_memory, new_len)) |new_items| {
1022 if (gpa.remap(old_memory, new_len)) |new_items| {
10251023 self.capacity = new_items.len;
10261024 self.items = new_items;
10271025 return;
10281026 }
10291027
1030 const new_memory = allocator.alignedAlloc(T, alignment, new_len) catch |e| switch (e) {
1028 const new_memory = gpa.alignedAlloc(T, alignment, new_len) catch |e| switch (e) {
10311029 error.OutOfMemory => {
10321030 // No problem, capacity is still correct then.
10331031 self.items.len = new_len;
......@@ -1036,7 +1034,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
10361034 };
10371035
10381036 @memcpy(new_memory, self.items[0..new_len]);
1039 allocator.free(old_memory);
1037 gpa.free(old_memory);
10401038 self.items = new_memory;
10411039 self.capacity = new_memory.len;
10421040 }
......@@ -1056,8 +1054,8 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
10561054 }
10571055
10581056 /// Invalidates all element pointers.
1059 pub fn clearAndFree(self: *Self, allocator: Allocator) void {
1060 allocator.free(self.allocatedSlice());
1057 pub fn clearAndFree(self: *Self, gpa: Allocator) void {
1058 gpa.free(self.allocatedSlice());
10611059 self.items.len = 0;
10621060 self.capacity = 0;
10631061 }
......@@ -1073,7 +1071,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
10731071 /// If the current capacity is less than `new_capacity`, this function will
10741072 /// modify the array so that it can hold exactly `new_capacity` items.
10751073 /// Invalidates element pointers if additional memory is needed.
1076 pub fn ensureTotalCapacityPrecise(self: *Self, allocator: Allocator, new_capacity: usize) Allocator.Error!void {
1074 pub fn ensureTotalCapacityPrecise(self: *Self, gpa: Allocator, new_capacity: usize) Allocator.Error!void {
10771075 if (@sizeOf(T) == 0) {
10781076 self.capacity = math.maxInt(usize);
10791077 return;
......@@ -1087,13 +1085,13 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
10871085 // the allocator implementation would pointlessly copy our
10881086 // extra capacity.
10891087 const old_memory = self.allocatedSlice();
1090 if (allocator.remap(old_memory, new_capacity)) |new_memory| {
1088 if (gpa.remap(old_memory, new_capacity)) |new_memory| {
10911089 self.items.ptr = new_memory.ptr;
10921090 self.capacity = new_memory.len;
10931091 } else {
1094 const new_memory = try allocator.alignedAlloc(T, alignment, new_capacity);
1092 const new_memory = try gpa.alignedAlloc(T, alignment, new_capacity);
10951093 @memcpy(new_memory[0..self.items.len], self.items);
1096 allocator.free(old_memory);
1094 gpa.free(old_memory);
10971095 self.items.ptr = new_memory.ptr;
10981096 self.capacity = new_memory.len;
10991097 }
......@@ -1103,10 +1101,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
11031101 /// Invalidates element pointers if additional memory is needed.
11041102 pub fn ensureUnusedCapacity(
11051103 self: *Self,
1106 allocator: Allocator,
1104 gpa: Allocator,
11071105 additional_count: usize,
11081106 ) Allocator.Error!void {
1109 return self.ensureTotalCapacity(allocator, try addOrOom(self.items.len, additional_count));
1107 return self.ensureTotalCapacity(gpa, try addOrOom(self.items.len, additional_count));
11101108 }
11111109
11121110 /// Increases the array's length to match the full capacity that is already allocated.
......@@ -1118,10 +1116,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
11181116
11191117 /// Increase length by 1, returning pointer to the new item.
11201118 /// The returned element pointer becomes invalid when the list is resized.
1121 pub fn addOne(self: *Self, allocator: Allocator) Allocator.Error!*T {
1119 pub fn addOne(self: *Self, gpa: Allocator) Allocator.Error!*T {
11221120 // This can never overflow because `self.items` can never occupy the whole address space
11231121 const newlen = self.items.len + 1;
1124 try self.ensureTotalCapacity(allocator, newlen);
1122 try self.ensureTotalCapacity(gpa, newlen);
11251123 return self.addOneAssumeCapacity();
11261124 }
11271125
......@@ -1139,9 +1137,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
11391137 /// Resize the array, adding `n` new elements, which have `undefined` values.
11401138 /// The return value is an array pointing to the newly allocated elements.
11411139 /// The returned pointer becomes invalid when the list is resized.
1142 pub fn addManyAsArray(self: *Self, allocator: Allocator, comptime n: usize) Allocator.Error!*[n]T {
1140 pub fn addManyAsArray(self: *Self, gpa: Allocator, comptime n: usize) Allocator.Error!*[n]T {
11431141 const prev_len = self.items.len;
1144 try self.resize(allocator, try addOrOom(self.items.len, n));
1142 try self.resize(gpa, try addOrOom(self.items.len, n));
11451143 return self.items[prev_len..][0..n];
11461144 }
11471145
......@@ -1161,9 +1159,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
11611159 /// The return value is a slice pointing to the newly allocated elements.
11621160 /// The returned pointer becomes invalid when the list is resized.
11631161 /// Resizes list if `self.capacity` is not large enough.
1164 pub fn addManyAsSlice(self: *Self, allocator: Allocator, n: usize) Allocator.Error![]T {
1162 pub fn addManyAsSlice(self: *Self, gpa: Allocator, n: usize) Allocator.Error![]T {
11651163 const prev_len = self.items.len;
1166 try self.resize(allocator, try addOrOom(self.items.len, n));
1164 try self.resize(gpa, try addOrOom(self.items.len, n));
11671165 return self.items[prev_len..][0..n];
11681166 }
11691167
lib/std/multi_array_list.zig+2-2
......@@ -248,8 +248,8 @@ pub fn MultiArrayList(comptime T: type) type {
248248 /// Extend the list by 1 element, returning the newly reserved
249249 /// index with uninitialized data.
250250 /// Allocates more memory as necesasry.
251 pub fn addOne(self: *Self, allocator: Allocator) Allocator.Error!usize {
252 try self.ensureUnusedCapacity(allocator, 1);
251 pub fn addOne(self: *Self, gpa: Allocator) Allocator.Error!usize {
252 try self.ensureUnusedCapacity(gpa, 1);
253253 return self.addOneAssumeCapacity();
254254 }
255255