diff --git a/util/buffer.c b/util/buffer.c index ae2907ebd4..31f1d9f286 100644 --- a/util/buffer.c +++ b/util/buffer.c @@ -24,6 +24,13 @@ #define BUFFER_MIN_INIT_SIZE 4096 #define BUFFER_MIN_SHRINK_SIZE 65536 +static size_t buffer_req_size(Buffer *buffer, size_t len) +{ + return MAX(BUFFER_MIN_INIT_SIZE, + pow2ceil(buffer->offset + len)); +} + + void buffer_init(Buffer *buffer, const char *name, ...) { va_list ap; @@ -61,8 +68,7 @@ void buffer_reserve(Buffer *buffer, size_t len) if ((buffer->capacity - buffer->offset) < len) { old = buffer->capacity; - buffer->capacity = pow2ceil(buffer->offset + len); - buffer->capacity = MAX(buffer->capacity, BUFFER_MIN_INIT_SIZE); + buffer->capacity = buffer_req_size(buffer, len); buffer->buffer = g_realloc(buffer->buffer, buffer->capacity); trace_buffer_resize(buffer->name ?: "unnamed", old, buffer->capacity);