| ... | ... | @@ -43,6 +43,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 43 | 43 | capacity: usize, |
| 44 | 44 | allocator: Allocator, |
| 45 | 45 | |
| 46 | /// Used to detect memory safety violations. |
| 47 | pointer_stability: debug.SafetyLock = .{}, |
| 48 | |
| 46 | 49 | pub const Slice = if (alignment) |a| ([]align(a.toByteUnits()) T) else []T; |
| 47 | 50 | |
| 48 | 51 | pub fn SentinelSlice(comptime s: T) type { |
| ... | ... | @@ -69,11 +72,29 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 69 | 72 | |
| 70 | 73 | /// Release all allocated memory. |
| 71 | 74 | pub fn deinit(self: Self) void { |
| 75 | self.pointer_stability.assertUnlocked(); |
| 72 | 76 | if (@sizeOf(T) > 0) { |
| 73 | 77 | self.allocator.free(self.allocatedSlice()); |
| 74 | 78 | } |
| 75 | 79 | } |
| 76 | 80 | |
| 81 | /// Puts the array list into a state where any method call that would |
| 82 | /// cause an existing value pointer to become invalidated will |
| 83 | /// instead trigger an assertion. |
| 84 | /// |
| 85 | /// An additional call to `lockPointers` in such state also triggers an |
| 86 | /// assertion. |
| 87 | /// |
| 88 | /// `unlockPointers` returns the array list to the previous state. |
| 89 | pub fn lockPointers(self: *Self) void { |
| 90 | self.pointer_stability.lock(); |
| 91 | } |
| 92 | |
| 93 | /// Undoes a call to `lockPointers`. |
| 94 | pub fn unlockPointers(self: *Self) void { |
| 95 | self.pointer_stability.unlock(); |
| 96 | } |
| 97 | |
| 77 | 98 | /// ArrayList takes ownership of the passed in slice. The slice must have been |
| 78 | 99 | /// allocated with `gpa`. |
| 79 | 100 | /// Deinitialize with `deinit` or use `toOwnedSlice`. |
| ... | ... | @@ -99,6 +120,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 99 | 120 | /// Initializes an ArrayListUnmanaged with the `items` and `capacity` fields |
| 100 | 121 | /// of this ArrayList. Empties this ArrayList. |
| 101 | 122 | pub fn moveToUnmanaged(self: *Self) ArrayListAlignedUnmanaged(T, alignment) { |
| 123 | self.pointer_stability.assertUnlocked(); |
| 102 | 124 | const allocator = self.allocator; |
| 103 | 125 | const result: ArrayListAlignedUnmanaged(T, alignment) = .{ .items = self.items, .capacity = self.capacity }; |
| 104 | 126 | self.* = init(allocator); |
| ... | ... | @@ -155,6 +177,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 155 | 177 | /// Asserts that there is enough capacity for the new item. |
| 156 | 178 | /// Asserts that the index is in bounds or equal to the length. |
| 157 | 179 | pub fn insertAssumeCapacity(self: *Self, i: usize, item: T) void { |
| 180 | self.pointer_stability.lock(); |
| 181 | defer self.pointer_stability.unlock(); |
| 182 | |
| 158 | 183 | assert(self.items.len < self.capacity); |
| 159 | 184 | self.items.len += 1; |
| 160 | 185 | |
| ... | ... | @@ -189,6 +214,8 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 189 | 214 | return addManyAtAssumeCapacity(self, index, count); |
| 190 | 215 | } |
| 191 | 216 | |
| 217 | self.pointer_stability.lock(); |
| 218 | defer self.pointer_stability.unlock(); |
| 192 | 219 | // Make a new allocation, avoiding `ensureTotalCapacity` in order |
| 193 | 220 | // to avoid extra memory copies. |
| 194 | 221 | const new_memory = try self.allocator.alignedAlloc(T, alignment, new_capacity); |
| ... | ... | @@ -212,6 +239,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 212 | 239 | /// does not invalidate any before that. |
| 213 | 240 | /// Asserts that the index is in bounds or equal to the length. |
| 214 | 241 | pub fn addManyAtAssumeCapacity(self: *Self, index: usize, count: usize) []T { |
| 242 | self.pointer_stability.lock(); |
| 243 | defer self.pointer_stability.unlock(); |
| 244 | |
| 215 | 245 | const new_len = self.items.len + count; |
| 216 | 246 | assert(self.capacity >= new_len); |
| 217 | 247 | const to_move = self.items[index..]; |
| ... | ... | @@ -419,17 +449,25 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 419 | 449 | /// Invalidates element pointers for the elements `items[new_len..]`. |
| 420 | 450 | /// Asserts that the new length is less than or equal to the previous length. |
| 421 | 451 | pub fn shrinkRetainingCapacity(self: *Self, new_len: usize) void { |
| 452 | self.pointer_stability.lock(); |
| 453 | defer self.pointer_stability.unlock(); |
| 454 | |
| 422 | 455 | assert(new_len <= self.items.len); |
| 423 | 456 | self.items.len = new_len; |
| 424 | 457 | } |
| 425 | 458 | |
| 426 | 459 | /// Invalidates all element pointers. |
| 427 | 460 | pub fn clearRetainingCapacity(self: *Self) void { |
| 461 | self.pointer_stability.lock(); |
| 462 | defer self.pointer_stability.unlock(); |
| 463 | |
| 428 | 464 | self.items.len = 0; |
| 429 | 465 | } |
| 430 | 466 | |
| 431 | 467 | /// Invalidates all element pointers. |
| 432 | 468 | pub fn clearAndFree(self: *Self) void { |
| 469 | self.pointer_stability.assertUnlocked(); |
| 470 | |
| 433 | 471 | self.allocator.free(self.allocatedSlice()); |
| 434 | 472 | self.items.len = 0; |
| 435 | 473 | self.capacity = 0; |
| ... | ... | @@ -454,6 +492,9 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 454 | 492 | /// modify the array so that it can hold exactly `new_capacity` items. |
| 455 | 493 | /// Invalidates element pointers if additional memory is needed. |
| 456 | 494 | pub fn ensureTotalCapacityPrecise(self: *Self, new_capacity: usize) Allocator.Error!void { |
| 495 | self.pointer_stability.lock(); |
| 496 | defer self.pointer_stability.unlock(); |
| 497 | |
| 457 | 498 | if (@sizeOf(T) == 0) { |
| 458 | 499 | self.capacity = math.maxInt(usize); |
| 459 | 500 | return; |
| ... | ... | @@ -559,6 +600,10 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?mem.Alignment) ty |
| 559 | 600 | /// Invalidates element pointers to the removed element, if any. |
| 560 | 601 | pub fn pop(self: *Self) ?T { |
| 561 | 602 | if (self.items.len == 0) return null; |
| 603 | |
| 604 | self.pointer_stability.lock(); |
| 605 | defer self.pointer_stability.unlock(); |
| 606 | |
| 562 | 607 | const val = self.items[self.items.len - 1]; |
| 563 | 608 | self.items.len -= 1; |
| 564 | 609 | return val; |
| ... | ... | @@ -631,6 +676,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig |
| 631 | 676 | /// additional memory. |
| 632 | 677 | capacity: usize = 0, |
| 633 | 678 | |
| 679 | /// Used to detect memory safety violations. |
| 680 | pointer_stability: debug.SafetyLock = .{}, |
| 681 | |
| 634 | 682 | /// An ArrayList containing no elements. |
| 635 | 683 | pub const empty: Self = .{ |
| 636 | 684 | .items = &.{}, |
| ... | ... | @@ -665,10 +713,28 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig |
| 665 | 713 | |
| 666 | 714 | /// Release all allocated memory. |
| 667 | 715 | pub fn deinit(self: *Self, gpa: Allocator) void { |
| 716 | self.pointer_stability.assertUnlocked(); |
| 668 | 717 | gpa.free(self.allocatedSlice()); |
| 669 | 718 | self.* = undefined; |
| 670 | 719 | } |
| 671 | 720 | |
| 721 | /// Puts the unmanaged array list into a state where any method call that would |
| 722 | /// cause an existing value pointer to become invalidated will |
| 723 | /// instead trigger an assertion. |
| 724 | /// |
| 725 | /// An additional call to `lockPointers` in such state also triggers an |
| 726 | /// assertion. |
| 727 | /// |
| 728 | /// `unlockPointers` returns the unmanaged array list to the previous state. |
| 729 | pub fn lockPointers(self: *Self) void { |
| 730 | self.pointer_stability.lock(); |
| 731 | } |
| 732 | |
| 733 | /// Undoes a call to `lockPointers`. |
| 734 | pub fn unlockPointers(self: *Self) void { |
| 735 | self.pointer_stability.unlock(); |
| 736 | } |
| 737 | |
| 672 | 738 | /// Convert this list into an analogous memory-managed one. |
| 673 | 739 | /// The returned list has ownership of the underlying memory. |
| 674 | 740 | pub fn toManaged(self: *Self, gpa: Allocator) ArrayListAligned(T, alignment) { |
| ... | ... | @@ -740,6 +806,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig |
| 740 | 806 | /// Asserts that the list has capacity for one additional item. |
| 741 | 807 | /// Asserts that the index is in bounds or equal to the length. |
| 742 | 808 | pub fn insertAssumeCapacity(self: *Self, i: usize, item: T) void { |
| 809 | self.pointer_stability.lock(); |
| 810 | defer self.pointer_stability.unlock(); |
| 811 | |
| 743 | 812 | assert(self.items.len < self.capacity); |
| 744 | 813 | self.items.len += 1; |
| 745 | 814 | |
| ... | ... | @@ -775,6 +844,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig |
| 775 | 844 | /// Asserts that the list has capacity for the additional items. |
| 776 | 845 | /// Asserts that the index is in bounds or equal to the length. |
| 777 | 846 | pub fn addManyAtAssumeCapacity(self: *Self, index: usize, count: usize) []T { |
| 847 | self.pointer_stability.lock(); |
| 848 | defer self.pointer_stability.unlock(); |
| 849 | |
| 778 | 850 | const new_len = self.items.len + count; |
| 779 | 851 | assert(self.capacity >= new_len); |
| 780 | 852 | const to_move = self.items[index..]; |
| ... | ... | @@ -843,6 +915,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig |
| 843 | 915 | const dst = self.addManyAtAssumeCapacity(after_range, rest.len); |
| 844 | 916 | @memcpy(dst, rest); |
| 845 | 917 | } else { |
| 918 | self.pointer_stability.lock(); |
| 919 | defer self.pointer_stability.unlock(); |
| 920 | |
| 846 | 921 | const extra = range.len - new_items.len; |
| 847 | 922 | @memcpy(range[0..new_items.len], new_items); |
| 848 | 923 | std.mem.copyForwards( |
| ... | ... | @@ -1044,17 +1119,26 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig |
| 1044 | 1119 | /// Keeps capacity the same. |
| 1045 | 1120 | /// Asserts that the new length is less than or equal to the previous length. |
| 1046 | 1121 | pub fn shrinkRetainingCapacity(self: *Self, new_len: usize) void { |
| 1122 | self.pointer_stability.lock(); |
| 1123 | defer self.pointer_stability.unlock(); |
| 1124 | |
| 1047 | 1125 | assert(new_len <= self.items.len); |
| 1048 | 1126 | self.items.len = new_len; |
| 1049 | 1127 | } |
| 1050 | 1128 | |
| 1051 | 1129 | /// Invalidates all element pointers. |
| 1052 | 1130 | pub fn clearRetainingCapacity(self: *Self) void { |
| 1131 | self.pointer_stability.lock(); |
| 1132 | defer self.pointer_stability.unlock(); |
| 1133 | |
| 1053 | 1134 | self.items.len = 0; |
| 1054 | 1135 | } |
| 1055 | 1136 | |
| 1056 | 1137 | /// Invalidates all element pointers. |
| 1057 | 1138 | pub fn clearAndFree(self: *Self, gpa: Allocator) void { |
| 1139 | self.pointer_stability.lock(); |
| 1140 | defer self.pointer_stability.unlock(); |
| 1141 | |
| 1058 | 1142 | gpa.free(self.allocatedSlice()); |
| 1059 | 1143 | self.items.len = 0; |
| 1060 | 1144 | self.capacity = 0; |
| ... | ... | @@ -1072,6 +1156,9 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig |
| 1072 | 1156 | /// modify the array so that it can hold exactly `new_capacity` items. |
| 1073 | 1157 | /// Invalidates element pointers if additional memory is needed. |
| 1074 | 1158 | pub fn ensureTotalCapacityPrecise(self: *Self, gpa: Allocator, new_capacity: usize) Allocator.Error!void { |
| 1159 | self.pointer_stability.lock(); |
| 1160 | defer self.pointer_stability.unlock(); |
| 1161 | |
| 1075 | 1162 | if (@sizeOf(T) == 0) { |
| 1076 | 1163 | self.capacity = math.maxInt(usize); |
| 1077 | 1164 | return; |
| ... | ... | @@ -1182,6 +1269,10 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?mem.Alig |
| 1182 | 1269 | /// Invalidates pointers to last element. |
| 1183 | 1270 | pub fn pop(self: *Self) ?T { |
| 1184 | 1271 | if (self.items.len == 0) return null; |
| 1272 | |
| 1273 | self.pointer_stability.lock(); |
| 1274 | defer self.pointer_stability.unlock(); |
| 1275 | |
| 1185 | 1276 | const val = self.items[self.items.len - 1]; |
| 1186 | 1277 | self.items.len -= 1; |
| 1187 | 1278 | return val; |