msg: improve comments on reallocation

Signed-off-by: Francis Bouvier <francis@lightpanda.io>
This commit is contained in:
Francis Bouvier
2024-10-11 18:05:04 +02:00
parent 4b495f213f
commit fd6c25daaa

View File

@@ -95,8 +95,11 @@ pub const MsgBuffer = struct {
// check if the current input can fit in MsgBuffer // check if the current input can fit in MsgBuffer
if (new_pos > self.buf.len) { if (new_pos > self.buf.len) {
// max_size is the max between msg size and current new cursor position // we want to realloc at least:
const max_size = @max(self.size, new_pos); // - 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 // resize the MsgBuffer to fit
self.buf = try alloc.realloc(self.buf, max_size); self.buf = try alloc.realloc(self.buf, max_size);
} }