aboutsummaryrefslogtreecommitdiff
path: root/src/proto/proto.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-28 10:54:58 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-28 10:54:58 +0100
commit35494bc81b59165ee9264cd1004bb05a120279a3 (patch)
tree8d11946f8fc20e1f254301100e860f28bade5ee0 /src/proto/proto.zig
parent0c063d42430077881e563120ebfcf92c2cecf463 (diff)
downloadpgz-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/proto.zig')
-rw-r--r--src/proto/proto.zig2
1 files changed, 1 insertions, 1 deletions
diff --git a/src/proto/proto.zig b/src/proto/proto.zig
index 5e4489d..9025347 100644
--- a/src/proto/proto.zig
+++ b/src/proto/proto.zig
@@ -51,13 +51,13 @@ pub fn read_message(allocator: std.mem.Allocator, stream_reader: anytype) !Backe
const tag = try stream_reader.readByte();
const len = try stream_reader.readIntBig(u32);
const buf = try allocator.alloc(u8, @as(u32, @intCast(len - 4)));
- defer allocator.free(buf);
try stream_reader.readNoEof(buf);
inline for (@typeInfo(BackendMessage).Union.fields) |field| {
if (field.type.Tag == tag) {
return @unionInit(BackendMessage, field.name, try field.type.read(allocator, buf));
}
} else {
+ allocator.free(buf);
return ProtocolError.InvalidMessageType;
}
}