| ... | ... | @@ -27,7 +27,6 @@ fn never_index_default(name: []const u8) bool { |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | const HeaderEntry = struct { |
| 30 | | allocator: *Allocator, |
| 31 | 30 | name: []const u8, |
| 32 | 31 | value: []u8, |
| 33 | 32 | never_index: bool, |
| ... | ... | @@ -36,23 +35,22 @@ const HeaderEntry = struct { |
| 36 | 35 | |
| 37 | 36 | fn init(allocator: *Allocator, name: []const u8, value: []const u8, never_index: ?bool) !Self { |
| 38 | 37 | return Self{ |
| 39 | | .allocator = allocator, |
| 40 | 38 | .name = name, // takes reference |
| 41 | 39 | .value = try allocator.dupe(u8, value), |
| 42 | 40 | .never_index = never_index orelse never_index_default(name), |
| 43 | 41 | }; |
| 44 | 42 | } |
| 45 | 43 | |
| 46 | | fn deinit(self: Self) void { |
| 47 | | self.allocator.free(self.value); |
| 44 | fn deinit(self: Self, allocator: *Allocator) void { |
| 45 | allocator.free(self.value); |
| 48 | 46 | } |
| 49 | 47 | |
| 50 | | pub fn modify(self: *Self, value: []const u8, never_index: ?bool) !void { |
| 48 | pub fn modify(self: *Self, allocator: *Allocator, value: []const u8, never_index: ?bool) !void { |
| 51 | 49 | const old_len = self.value.len; |
| 52 | 50 | if (value.len > old_len) { |
| 53 | | self.value = try self.allocator.realloc(self.value, value.len); |
| 51 | self.value = try allocator.realloc(self.value, value.len); |
| 54 | 52 | } else if (value.len < old_len) { |
| 55 | | self.value = self.allocator.shrink(self.value, value.len); |
| 53 | self.value = allocator.shrink(self.value, value.len); |
| 56 | 54 | } |
| 57 | 55 | mem.copy(u8, self.value, value); |
| 58 | 56 | self.never_index = never_index orelse never_index_default(self.name); |
| ... | ... | @@ -85,16 +83,16 @@ const HeaderEntry = struct { |
| 85 | 83 | |
| 86 | 84 | test "HeaderEntry" { |
| 87 | 85 | var e = try HeaderEntry.init(testing.allocator, "foo", "bar", null); |
| 88 | | defer e.deinit(); |
| 86 | defer e.deinit(testing.allocator); |
| 89 | 87 | testing.expectEqualSlices(u8, "foo", e.name); |
| 90 | 88 | testing.expectEqualSlices(u8, "bar", e.value); |
| 91 | 89 | testing.expectEqual(false, e.never_index); |
| 92 | 90 | |
| 93 | | try e.modify("longer value", null); |
| 91 | try e.modify(testing.allocator, "longer value", null); |
| 94 | 92 | testing.expectEqualSlices(u8, "longer value", e.value); |
| 95 | 93 | |
| 96 | 94 | // shorter value |
| 97 | | try e.modify("x", null); |
| 95 | try e.modify(testing.allocator, "x", null); |
| 98 | 96 | testing.expectEqualSlices(u8, "x", e.value); |
| 99 | 97 | } |
| 100 | 98 | |
| ... | ... | @@ -129,7 +127,7 @@ pub const Headers = struct { |
| 129 | 127 | } |
| 130 | 128 | { |
| 131 | 129 | for (self.data.items) |entry| { |
| 132 | | entry.deinit(); |
| 130 | entry.deinit(self.allocator); |
| 133 | 131 | } |
| 134 | 132 | self.data.deinit(self.allocator); |
| 135 | 133 | } |
| ... | ... | @@ -157,14 +155,14 @@ pub const Headers = struct { |
| 157 | 155 | var entry: HeaderEntry = undefined; |
| 158 | 156 | if (self.index.getEntry(name)) |kv| { |
| 159 | 157 | entry = try HeaderEntry.init(self.allocator, kv.key, value, never_index); |
| 160 | | errdefer entry.deinit(); |
| 158 | errdefer entry.deinit(self.allocator); |
| 161 | 159 | const dex = &kv.value; |
| 162 | 160 | try dex.append(self.allocator, n - 1); |
| 163 | 161 | } else { |
| 164 | 162 | const name_dup = try self.allocator.dupe(u8, name); |
| 165 | 163 | errdefer self.allocator.free(name_dup); |
| 166 | 164 | entry = try HeaderEntry.init(self.allocator, name_dup, value, never_index); |
| 167 | | errdefer entry.deinit(); |
| 165 | errdefer entry.deinit(self.allocator); |
| 168 | 166 | var dex = HeaderIndexList{}; |
| 169 | 167 | try dex.append(self.allocator, n - 1); |
| 170 | 168 | errdefer dex.deinit(self.allocator); |
| ... | ... | @@ -203,7 +201,7 @@ pub const Headers = struct { |
| 203 | 201 | const data_index = dex.items[i]; |
| 204 | 202 | const removed = self.data.orderedRemove(data_index); |
| 205 | 203 | assert(mem.eql(u8, removed.name, name)); |
| 206 | | removed.deinit(); |
| 204 | removed.deinit(self.allocator); |
| 207 | 205 | } |
| 208 | 206 | dex.deinit(self.allocator); |
| 209 | 207 | self.allocator.free(kv.key); |
| ... | ... | @@ -226,13 +224,13 @@ pub const Headers = struct { |
| 226 | 224 | if (dex.items.len == 1) { |
| 227 | 225 | // was last item; delete the index |
| 228 | 226 | dex.deinit(self.allocator); |
| 229 | | removed.deinit(); |
| 227 | removed.deinit(self.allocator); |
| 230 | 228 | const key = kv.key; |
| 231 | 229 | _ = self.index.remove(key); // invalidates `kv` and `dex` |
| 232 | 230 | self.allocator.free(key); |
| 233 | 231 | } else { |
| 234 | 232 | dex.shrink(self.allocator, dex.items.len - 1); |
| 235 | | removed.deinit(); |
| 233 | removed.deinit(self.allocator); |
| 236 | 234 | } |
| 237 | 235 | // if it was the last item; no need to rebuild index |
| 238 | 236 | if (i != self.data.items.len) { |
| ... | ... | @@ -251,13 +249,13 @@ pub const Headers = struct { |
| 251 | 249 | if (dex.items.len == 1) { |
| 252 | 250 | // was last item; delete the index |
| 253 | 251 | dex.deinit(self.allocator); |
| 254 | | removed.deinit(); |
| 252 | removed.deinit(self.allocator); |
| 255 | 253 | const key = kv.key; |
| 256 | 254 | _ = self.index.remove(key); // invalidates `kv` and `dex` |
| 257 | 255 | self.allocator.free(key); |
| 258 | 256 | } else { |
| 259 | 257 | dex.shrink(self.allocator, dex.items.len - 1); |
| 260 | | removed.deinit(); |
| 258 | removed.deinit(self.allocator); |
| 261 | 259 | } |
| 262 | 260 | // if it was the last item; no need to rebuild index |
| 263 | 261 | if (i != self.data.items.len) { |