| ... | @@ -773,9 +773,9 @@ pub fn ArrayHashMapUnmanaged( | ... | @@ -773,9 +773,9 @@ pub fn ArrayHashMapUnmanaged( |
| 773 | } | 773 | } |
| 774 | } | 774 | } |
| 775 | | 775 | |
| | 776 | try self.entries.ensureTotalCapacity(allocator, new_capacity); |
| 776 | const new_bit_index = try IndexHeader.findBitIndex(new_capacity); | 777 | const new_bit_index = try IndexHeader.findBitIndex(new_capacity); |
| 777 | const new_header = try IndexHeader.alloc(allocator, new_bit_index); | 778 | const new_header = try IndexHeader.alloc(allocator, new_bit_index); |
| 778 | try self.entries.ensureTotalCapacity(allocator, new_capacity); | | |
| 779 | | 779 | |
| 780 | if (self.index_header) |old_header| old_header.free(allocator); | 780 | if (self.index_header) |old_header| old_header.free(allocator); |
| 781 | self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, new_header); | 781 | self.insertAllEntriesIntoNewHeader(if (store_hash) {} else ctx, new_header); |
| ... | @@ -2042,6 +2042,19 @@ test "ensure capacity" { | ... | @@ -2042,6 +2042,19 @@ test "ensure capacity" { |
| 2042 | try testing.expect(initial_capacity == map.capacity()); | 2042 | try testing.expect(initial_capacity == map.capacity()); |
| 2043 | } | 2043 | } |
| 2044 | | 2044 | |
| | 2045 | test "ensure capacity leak" { |
| | 2046 | try testing.checkAllAllocationFailures(std.testing.allocator, struct { |
| | 2047 | pub fn f(allocator: Allocator) !void { |
| | 2048 | var map = AutoArrayHashMap(i32, i32).init(allocator); |
| | 2049 | defer map.deinit(); |
| | 2050 | |
| | 2051 | var i: i32 = 0; |
| | 2052 | // put more than `linear_scan_max` in so index_header gets allocated. |
| | 2053 | while (i <= 20) : (i += 1) try map.put(i, i); |
| | 2054 | } |
| | 2055 | }.f, .{}); |
| | 2056 | } |
| | 2057 | |
| 2045 | test "big map" { | 2058 | test "big map" { |
| 2046 | var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator); | 2059 | var map = AutoArrayHashMap(i32, i32).init(std.testing.allocator); |
| 2047 | defer map.deinit(); | 2060 | defer map.deinit(); |