On Thu, 26 Aug 2021 at 18:05, Matteo Bruni mbruni@codeweavers.com wrote:
static bool vkd3d_string_buffer_resize(struct vkd3d_string_buffer *buffer, int rc) {
- unsigned int new_buffer_size = buffer->buffer_size * 2;
- char *new_buffer;
- unsigned int new_buffer_size = max(buffer->buffer_size * 2, 32);
- if (rc >= 0 && rc < buffer->buffer_size - buffer->content_size)
return true;
- new_buffer_size = max(new_buffer_size, 32); while (rc > 0 && (unsigned int)rc >= new_buffer_size - buffer->content_size) new_buffer_size *= 2;
- if (!(new_buffer = vkd3d_realloc(buffer->buffer, new_buffer_size)))
- if (!vkd3d_array_reserve((void **)&buffer->buffer, &buffer->buffer_size, new_buffer_size, 1)) { ERR("Failed to grow buffer.\n"); buffer->buffer[buffer->content_size] = '\0'; return false; }
- buffer->buffer = new_buffer;
- buffer->buffer_size = new_buffer_size; return true;
}
Note that now that we're using vkd3d_array_reserve(), we no longer need the "while (rc > 0 && ...)" loop, or the size check you're introducing. In the rc >= 0 case, we can pass "buffer->content_size + rc" to vkd3d_array_reserve(), and it will take care of all that. If rc < 0, we pass "max(buffer->buffer_size * 2, 32)" instead.