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). v4: Make buffer_size and content_size size_t.
libs/vkd3d-shader/vkd3d_shader_main.c | 10 ++-------- libs/vkd3d-shader/vkd3d_shader_private.h | 3 +-- 2 files changed, 3 insertions(+), 10 deletions(-)
diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index e38261b4..baf25612 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; }
diff --git a/libs/vkd3d-shader/vkd3d_shader_private.h b/libs/vkd3d-shader/vkd3d_shader_private.h index 8b3e7624..fa56ba1d 100644 --- a/libs/vkd3d-shader/vkd3d_shader_private.h +++ b/libs/vkd3d-shader/vkd3d_shader_private.h @@ -912,8 +912,7 @@ void shader_sm4_reset(struct vkd3d_shader_parser *parser); struct vkd3d_string_buffer { char *buffer; - unsigned int buffer_size; - unsigned int content_size; + size_t buffer_size, content_size; };
struct vkd3d_string_buffer_cache
Signed-off-by: Matteo Bruni mbruni@codeweavers.com --- v2: Get rid of now unnecessary max() in vkd3d_string_buffer_resize() for the rc < 0 case.
libs/vkd3d-shader/vkd3d_shader_main.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
diff --git a/libs/vkd3d-shader/vkd3d_shader_main.c b/libs/vkd3d-shader/vkd3d_shader_main.c index baf25612..ef9eaef5 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) @@ -41,7 +45,7 @@ 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 = rc >= 0 ? buffer->content_size + rc + 1 : max(buffer->buffer_size * 2, 32); + unsigned int new_buffer_size = rc >= 0 ? buffer->content_size + rc + 1 : buffer->buffer_size * 2;
if (!vkd3d_array_reserve((void **)&buffer->buffer, &buffer->buffer_size, new_buffer_size, 1)) { @@ -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; @@ -144,11 +145,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 {
Signed-off-by: Henri Verbeet hverbeet@codeweavers.com