From: Bartosz Tomczyk Date: Mon, 3 Apr 2017 17:39:19 +0000 (+0200) Subject: glsl: Fix blob memory leak X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=bcb63ee63e0a8bec837ee3712e1d306409455a4a;p=mesa.git glsl: Fix blob memory leak Reviewed-by: Timothy Arceri --- diff --git a/src/compiler/glsl/blob.h b/src/compiler/glsl/blob.h index 9de12e6eb82..940c81e13b4 100644 --- a/src/compiler/glsl/blob.h +++ b/src/compiler/glsl/blob.h @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -79,6 +80,16 @@ struct blob_reader { struct blob * blob_create(void); +/** + * Destroy a blob and free its memory. + */ +static inline void +blob_destroy(struct blob *blob) +{ + free(blob->data); + free(blob); +} + /** * Add some unstructured, fixed-size data to a blob. * diff --git a/src/compiler/glsl/shader_cache.cpp b/src/compiler/glsl/shader_cache.cpp index ea1bc01f028..f5e6a22bb9e 100644 --- a/src/compiler/glsl/shader_cache.cpp +++ b/src/compiler/glsl/shader_cache.cpp @@ -1273,7 +1273,7 @@ shader_cache_write_program_metadata(struct gl_context *ctx, disk_cache_put(cache, prog->data->sha1, metadata->data, metadata->size); - free(metadata); + blob_destroy(metadata); if (ctx->_Shader->Flags & GLSL_CACHE_INFO) { _mesa_sha1_format(sha1_buf, prog->data->sha1); diff --git a/src/compiler/glsl/tests/blob_test.c b/src/compiler/glsl/tests/blob_test.c index 01b5ef0b2f0..df0e91af353 100644 --- a/src/compiler/glsl/tests/blob_test.c +++ b/src/compiler/glsl/tests/blob_test.c @@ -184,7 +184,7 @@ test_write_and_read_functions (void) "read_consumes_all_bytes"); expect_equal(false, reader.overrun, "read_does_not_overrun"); - free(blob); + blob_destroy(blob); } /* Test that data values are written and read with proper alignment. */ @@ -242,7 +242,7 @@ test_alignment(void) "aligned read of intptr_t"); } - free(blob); + blob_destroy(blob); } /* Test that we detect overrun. */ @@ -264,7 +264,7 @@ test_overrun(void) expect_equal(0, blob_read_uint32(&reader), "read at overrun"); expect_equal(true, reader.overrun, "overrun flag set"); - free(blob); + blob_destroy(blob); } /* Test that we can read and write some large objects, (exercising the code in @@ -308,7 +308,7 @@ test_big_objects(void) expect_equal(false, reader.overrun, "overrun flag not set reading large objects"); - free(blob); + blob_destroy(blob); ralloc_free(ctx); } diff --git a/src/mesa/state_tracker/st_shader_cache.c b/src/mesa/state_tracker/st_shader_cache.c index e8c7289ec6d..1a11f1135d7 100644 --- a/src/mesa/state_tracker/st_shader_cache.c +++ b/src/mesa/state_tracker/st_shader_cache.c @@ -135,7 +135,7 @@ st_store_tgsi_in_disk_cache(struct st_context *st, struct gl_program *prog, _mesa_shader_stage_to_string(prog->info.stage), sha1_buf); } - free(blob); + blob_destroy(blob); } static void