From fd6c25daaa8fe7fe52abcc4ca6b3c3f2b3d72444 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Fri, 11 Oct 2024 18:05:04 +0200 Subject: [PATCH] msg: improve comments on reallocation Signed-off-by: Francis Bouvier --- src/msg.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/msg.zig b/src/msg.zig index 6bcaa182..f76b5339 100644 --- a/src/msg.zig +++ b/src/msg.zig @@ -95,8 +95,11 @@ pub const MsgBuffer = struct { // check if the current input can fit in MsgBuffer if (new_pos > self.buf.len) { - // max_size is the max between msg size and current new cursor position - const max_size = @max(self.size, new_pos); + // we want to realloc at least: + // - a size equals to new_pos to fit the entire input + // - a size big enough (ie. current size + starting size) + // to avoid multiple reallocation + const max_size = @max(self.buf.len + self.size, new_pos); // resize the MsgBuffer to fit self.buf = try alloc.realloc(self.buf, max_size); }