diff options
author | Martin Ashby <martin@ashbysoft.com> | 2023-09-28 10:54:58 +0100 |
---|---|---|
committer | Martin Ashby <martin@ashbysoft.com> | 2023-09-28 10:54:58 +0100 |
commit | 35494bc81b59165ee9264cd1004bb05a120279a3 (patch) | |
tree | 8d11946f8fc20e1f254301100e860f28bade5ee0 /src/proto/backend_key_data.zig | |
parent | 0c063d42430077881e563120ebfcf92c2cecf463 (diff) | |
download | pgz-35494bc81b59165ee9264cd1004bb05a120279a3.tar.gz pgz-35494bc81b59165ee9264cd1004bb05a120279a3.tar.bz2 pgz-35494bc81b59165ee9264cd1004bb05a120279a3.tar.xz pgz-35494bc81b59165ee9264cd1004bb05a120279a3.zip |
Reduce allocations, the message takes ownership of the bytes read from
the stream and is responsible for deallocating, rather than copying them
to their own storage.
Diffstat (limited to 'src/proto/backend_key_data.zig')
-rw-r--r-- | src/proto/backend_key_data.zig | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/proto/backend_key_data.zig b/src/proto/backend_key_data.zig index 7c32178..4e7f30d 100644 --- a/src/proto/backend_key_data.zig +++ b/src/proto/backend_key_data.zig @@ -10,11 +10,12 @@ pub const Tag: u8 = 'K'; process_id: u32, secret_key: u32, -pub fn read(_: std.mem.Allocator, b: []const u8) !BackendKeyData { - if (b.len != 8) return ProtocolError.InvalidMessageLength; +pub fn read(a: std.mem.Allocator, buf: []const u8) !BackendKeyData { + defer a.free(buf); + if (buf.len != 8) return ProtocolError.InvalidMessageLength; return .{ - .process_id = std.mem.readIntBig(u32, b[0..4]), - .secret_key = std.mem.readIntBig(u32, b[4..8]), + .process_id = std.mem.readIntBig(u32, buf[0..4]), + .secret_key = std.mem.readIntBig(u32, buf[4..8]), }; } pub fn write(self: BackendKeyData, _: std.mem.Allocator, stream_writer: anytype) !void { @@ -43,7 +44,6 @@ test "round trip" { try std.testing.expectEqual(Tag, tag); const len = try reader.readIntBig(u32); const buf = try allocator.alloc(u8, len - 4); - defer allocator.free(buf); try reader.readNoEof(buf); var sm2 = try BackendKeyData.read(allocator, buf); defer sm2.deinit(allocator); |