aboutsummaryrefslogtreecommitdiff
path: root/src/proto/parameter_status.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/parameter_status.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/parameter_status.zig')
-rw-r--r--src/proto/parameter_status.zig18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/proto/parameter_status.zig b/src/proto/parameter_status.zig
index 5f95695..7bc306b 100644
--- a/src/proto/parameter_status.zig
+++ b/src/proto/parameter_status.zig
@@ -11,13 +11,14 @@ buf: ?[]const u8 = null, // owned
name: []const u8,
value: []const u8,
-pub fn read(allocator: std.mem.Allocator, b: []const u8) !ParameterStatus {
- var res: ParameterStatus = undefined;
- res.buf = try allocator.dupe(u8, b);
- var it = std.mem.splitScalar(u8, res.buf.?, 0);
- res.name = it.first();
- res.value = it.next() orelse return ProtocolError.MissingField;
- return res;
+pub fn read(a: std.mem.Allocator, buf: []const u8) !ParameterStatus {
+ errdefer a.free(buf);
+ var it = std.mem.splitScalar(u8, buf, 0);
+ return .{
+ .buf = buf,
+ .name = it.first(),
+ .value = it.next() orelse return ProtocolError.MissingField,
+ };
}
pub fn write(self: ParameterStatus, a: std.mem.Allocator, stream_writer: anytype) !void {
try stream_writer.writeByte(Tag);
@@ -30,7 +31,7 @@ pub fn write(self: ParameterStatus, a: std.mem.Allocator, stream_writer: anytype
try writer.writeByte(0);
try writer.writeAll(self.value);
try writer.writeByte(0);
- std.mem.writeIntBig(u32, al.items[0..4], @as(u32,@intCast(cw.bytes_written))); // Fix length
+ std.mem.writeIntBig(u32, al.items[0..4], @as(u32, @intCast(cw.bytes_written))); // Fix length
try stream_writer.writeAll(al.items);
}
pub fn deinit(self: *ParameterStatus, allocator: std.mem.Allocator) void {
@@ -55,7 +56,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 ParameterStatus.read(allocator, buf);
defer sm2.deinit(allocator);