authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-04-23 15:32:11-07:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2023-04-23 15:33:05-07:00
log1acb3162b7e24e6badad4aff37dbeac09f47f165
tree80022976e7a6792ba75397beb311ed7c17c4d48e
parent4374ce51b0ffdaa317b6678d6622ecb88877f7f9

http.Headers: Add `clearAndFree` and `clearRetainingCapacity`


1 files changed, 73 insertions(+), 11 deletions(-)

lib/std/http/Headers.zig+73-11
......@@ -68,17 +68,7 @@ pub const Headers = struct {
6868 }
6969
7070 pub fn deinit(headers: *Headers) void {
71 var it = headers.index.iterator();
72 while (it.next()) |entry| {
73 entry.value_ptr.deinit(headers.allocator);
74
75 if (headers.owned) headers.allocator.free(entry.key_ptr.*);
76 }
77
78 for (headers.list.items) |entry| {
79 if (headers.owned) headers.allocator.free(entry.value);
80 }
81
71 headers.deallocateIndexListsAndFields();
8272 headers.index.deinit(headers.allocator);
8373 headers.list.deinit(headers.allocator);
8474
......@@ -255,6 +245,39 @@ pub const Headers = struct {
255245
256246 try out_stream.writeAll("\r\n");
257247 }
248
249 /// Frees all `HeaderIndexList`s within `index`
250 /// Frees names and values of all fields if they are owned.
251 fn deallocateIndexListsAndFields(headers: *Headers) void {
252 var it = headers.index.iterator();
253 while (it.next()) |entry| {
254 entry.value_ptr.deinit(headers.allocator);
255
256 if (headers.owned) headers.allocator.free(entry.key_ptr.*);
257 }
258
259 if (headers.owned) {
260 for (headers.list.items) |entry| {
261 headers.allocator.free(entry.value);
262 }
263 }
264 }
265
266 /// Clears and frees the underlying data structures.
267 /// Frees names and values if they are owned.
268 pub fn clearAndFree(headers: *Headers) void {
269 headers.deallocateIndexListsAndFields();
270 headers.index.clearAndFree(headers.allocator);
271 headers.list.clearAndFree(headers.allocator);
272 }
273
274 /// Clears the underlying data structures while retaining their capacities.
275 /// Frees names and values if they are owned.
276 pub fn clearRetainingCapacity(headers: *Headers) void {
277 headers.deallocateIndexListsAndFields();
278 headers.index.clearRetainingCapacity();
279 headers.list.clearRetainingCapacity();
280 }
258281};
259282
260283test "Headers.append" {
......@@ -384,3 +407,42 @@ test "Headers consistency" {
384407 try h.formatCommaSeparated("foo", writer);
385408 try testing.expectEqualStrings("foo: bar, baz\r\n", fbs.getWritten());
386409}
410
411test "Headers.clearRetainingCapacity and clearAndFree" {
412 var h = Headers.init(std.testing.allocator);
413 defer h.deinit();
414
415 h.clearRetainingCapacity();
416
417 try h.append("foo", "bar");
418 try h.append("bar", "world");
419 try h.append("foo", "baz");
420 try h.append("baz", "hello");
421 try testing.expectEqual(@as(usize, 4), h.list.items.len);
422 try testing.expectEqual(@as(usize, 3), h.index.count());
423 const list_capacity = h.list.capacity;
424 const index_capacity = h.index.capacity();
425
426 h.clearRetainingCapacity();
427 try testing.expectEqual(@as(usize, 0), h.list.items.len);
428 try testing.expectEqual(@as(usize, 0), h.index.count());
429 try testing.expectEqual(list_capacity, h.list.capacity);
430 try testing.expectEqual(index_capacity, h.index.capacity());
431
432 try h.append("foo", "bar");
433 try h.append("bar", "world");
434 try h.append("foo", "baz");
435 try h.append("baz", "hello");
436 try testing.expectEqual(@as(usize, 4), h.list.items.len);
437 try testing.expectEqual(@as(usize, 3), h.index.count());
438 // Capacity should still be the same since we shouldn't have needed to grow
439 // when adding back the same fields
440 try testing.expectEqual(list_capacity, h.list.capacity);
441 try testing.expectEqual(index_capacity, h.index.capacity());
442
443 h.clearAndFree();
444 try testing.expectEqual(@as(usize, 0), h.list.items.len);
445 try testing.expectEqual(@as(usize, 0), h.index.count());
446 try testing.expectEqual(@as(usize, 0), h.list.capacity);
447 try testing.expectEqual(@as(usize, 0), h.index.capacity());
448}