Signed-off-by: Matteo Bruni mbruni@codeweavers.com --- v2: Handle rc < 0 case, use vkd3d_array_reserve(). v3: Fully make use of vkd3d_array_reserve() (thanks Henri).
libs/vkd3d-shader/vkd3d_shader_main.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-)
diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index 5d7cdf01..e7afbdfc 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -41,20 +41,14 @@ static void vkd3d_string_buffer_clear(struct vkd3d_string_buffer *buffer)
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 = rc >= 0 ? buffer->content_size + rc + 1 : max(buffer->buffer_size * 2, 32);
- 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; }
Signed-off-by: Matteo Bruni mbruni@codeweavers.com --- It turns out that the solution in v2 didn't quite work because preproc_add_macro() (and possibly others) copies a struct vkd3d_string_buffer around. Rather than making that invalid, let's go entirely the other way: always allocate a small buffer at init time and, if that fails, simply crash: the situation is hopeless.
libs/vkd3d-shader/vkd3d_shader_main.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index e7afbdfc..79bb9023 100644 --- a/libs/vkd3d-shader/vkd3d_shader_main.c +++ b/libs/vkd3d-shader/vkd3d_shader_main.c @@ -25,7 +25,11 @@ VKD3D_DEBUG_ENV_NAME("VKD3D_SHADER_DEBUG");
void vkd3d_string_buffer_init(struct vkd3d_string_buffer *buffer) { - memset(buffer, 0, sizeof(*buffer)); + buffer->buffer_size = 16; + buffer->content_size = 0; + buffer->buffer = vkd3d_malloc(buffer->buffer_size); + assert(buffer->buffer); + memset(buffer->buffer, 0, buffer->buffer_size); }
void vkd3d_string_buffer_cleanup(struct vkd3d_string_buffer *buffer) @@ -58,9 +62,6 @@ int vkd3d_string_buffer_vprintf(struct vkd3d_string_buffer *buffer, const char * va_list a; int rc;
- if (!buffer->content_size && !vkd3d_string_buffer_resize(buffer, 32)) - return -1; - for (;;) { rem = buffer->buffer_size - buffer->content_size; @@ -139,11 +140,6 @@ struct vkd3d_string_buffer *vkd3d_string_buffer_get(struct vkd3d_string_buffer_c if (!(buffer = vkd3d_malloc(sizeof(*buffer)))) return NULL; vkd3d_string_buffer_init(buffer); - if (!vkd3d_string_buffer_resize(buffer, 1)) - { - vkd3d_free(buffer); - return NULL; - } } else {
On Wed, 15 Sept 2021 at 10:19, 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 = rc >= 0 ? buffer->content_size + rc + 1 : max(buffer->buffer_size * 2, 32);
- 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))
Unfortunately this introduces warnings on 64-bit builds:
libs/vkd3d-shader/vkd3d_shader_main.c:50:56: warning: passing argument 2 of ‘vkd3d_array_reserve’ from incompatible pointer type [-Wincompatible-pointer-types] if (!vkd3d_array_reserve((void **)&buffer->buffer, &buffer->buffer_size, new_buffer_size, 1)) ^~~~~~~~~~~~~~~~~~~~