Module: Mesa
Branch: main
Commit: 000a8cbb65fd7353d9b743f8ec5c7b4edf57e473
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=000a8cbb65fd7353d9b743f8ec5c7b4edf57e473

Author: Pavel Ondračka <pavel.ondra...@gmail.com>
Date:   Mon Jan  8 16:43:43 2024 +0100

r300: fix memory leaks in compiler tests

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26823>

---

 .../compiler/tests/radeon_compiler_optimize_tests.c  |  2 ++
 .../compiler/tests/radeon_compiler_regalloc_tests.c  |  2 ++
 .../r300/compiler/tests/radeon_compiler_util_tests.c |  2 ++
 .../drivers/r300/compiler/tests/rc_test_helpers.c    | 20 +++++++++++++++++++-
 .../drivers/r300/compiler/tests/rc_test_helpers.h    |  2 ++
 5 files changed, 27 insertions(+), 1 deletion(-)

diff --git 
a/src/gallium/drivers/r300/compiler/tests/radeon_compiler_optimize_tests.c 
b/src/gallium/drivers/r300/compiler/tests/radeon_compiler_optimize_tests.c
index 7672f68d323..286a0eb4c0c 100644
--- a/src/gallium/drivers/r300/compiler/tests/radeon_compiler_optimize_tests.c
+++ b/src/gallium/drivers/r300/compiler/tests/radeon_compiler_optimize_tests.c
@@ -77,6 +77,8 @@ static void test_runner_rc_optimize(struct test_result * 
result)
        }
 
        test_check(result, pass);
+
+       destroy_compiler(&c);
 }
 
 unsigned radeon_compiler_optimize_run_tests()
diff --git 
a/src/gallium/drivers/r300/compiler/tests/radeon_compiler_regalloc_tests.c 
b/src/gallium/drivers/r300/compiler/tests/radeon_compiler_regalloc_tests.c
index e1b6b248fe2..5d82fa588cc 100644
--- a/src/gallium/drivers/r300/compiler/tests/radeon_compiler_regalloc_tests.c
+++ b/src/gallium/drivers/r300/compiler/tests/radeon_compiler_regalloc_tests.c
@@ -88,6 +88,8 @@ static void tex_1d_swizzle(struct test_result *result)
        c.AllocateHwInputs = dummy_allocate_hw_inputs;
 
        test_runner_rc_regalloc(result, &c.Base, 
"regalloc_tex_1d_swizzle.test");
+
+       destroy_compiler(&c.Base);
 }
 
 unsigned radeon_compiler_regalloc_run_tests()
diff --git 
a/src/gallium/drivers/r300/compiler/tests/radeon_compiler_util_tests.c 
b/src/gallium/drivers/r300/compiler/tests/radeon_compiler_util_tests.c
index bcdbbfc6f00..942370ffd07 100644
--- a/src/gallium/drivers/r300/compiler/tests/radeon_compiler_util_tests.c
+++ b/src/gallium/drivers/r300/compiler/tests/radeon_compiler_util_tests.c
@@ -57,6 +57,8 @@ static void test_rc_inst_can_use_presub(
                        &add_inst.U.I.SrcReg[0], &add_inst.U.I.SrcReg[1]);
 
        test_check(result, ret == expected);
+
+       destroy_compiler(&c.Base);
 }
 
 static void test_runner_rc_inst_can_use_presub(struct test_result * result)
diff --git a/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.c 
b/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.c
index 0c85579ca9c..fe5323c53b1 100644
--- a/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.c
+++ b/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.c
@@ -68,9 +68,12 @@ static int is_whitespace(const char *str)
        regex_t regex;
        if (regcomp(&regex, "^[ \n]+$", REG_EXTENDED)) {
                fprintf(stderr, "Failed to compile whitespace regex\n");
+               regfree(&regex);
                return 0;
        }
-       return regexec(&regex, str, 0, NULL, 0) != REG_NOMATCH;
+       bool ret = regexec(&regex, str, 0, NULL, 0) != REG_NOMATCH;
+       regfree(&regex);
+       return ret;
 }
 
 static int match_length(regmatch_t * matches, int index)
@@ -93,6 +96,7 @@ static int regex_helper(
        if (err_code) {
                regerror(err_code, &regex, err_buf, REGEX_ERR_BUF_SIZE);
                fprintf(stderr, "Failed to compile regex: %s\n", err_buf);
+               regfree(&regex);
                return 0;
        }
 
@@ -106,8 +110,10 @@ static int regex_helper(
        if (err_code) {
                regerror(err_code, &regex, err_buf, REGEX_ERR_BUF_SIZE);
                fprintf(stderr, "Failed to match regex: %s\n", err_buf);
+               regfree(&regex);
                return 0;
        }
+       regfree(&regex);
        return 1;
 }
 
@@ -427,6 +433,7 @@ int parse_rc_normal_instruction(
                                                tokens.Srcs[j].Length);
                        src_str[tokens.Srcs[j].Length] = '\0';
                        init_rc_normal_src(inst, j, src_str);
+                       free(src_str);
                }
                if (info->HasTexture) {
                        /* XXX: Will this always be XYZW ? */
@@ -530,6 +537,13 @@ void init_compiler(
        }
 }
 
+void destroy_compiler(struct radeon_compiler *c)
+{
+       rc_destroy_regalloc_state((struct rc_regalloc_state 
*)c->regalloc_state);
+       free((void *)c->regalloc_state);
+       rc_destroy(c);
+}
+
 #define MAX_LINE_LENGTH 100
 
 unsigned load_program(
@@ -608,12 +622,16 @@ unsigned load_program(
        for (i = 0; i < test->num_input_lines; i++) {
                if (test->input[i][0] == 'c') {
                        add_constant(c, test->input[i]);
+                       free(test->input[i]);
                        continue;
                }
                // XXX: Parse immediates from the file.
                add_instruction(c, test->input[i]);
+               free(test->input[i]);
        }
 
        fclose(file);
+       free(test->input);
+       free(test->expected);
        return 1;
 }
diff --git a/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.h 
b/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.h
index 6cc8d9cdcb4..a42486d865a 100644
--- a/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.h
+++ b/src/gallium/drivers/r300/compiler/tests/rc_test_helpers.h
@@ -65,6 +65,8 @@ void init_compiler(
        unsigned is_r500,
        unsigned is_r400);
 
+void destroy_compiler(struct radeon_compiler *c);
+
 unsigned load_program(
        struct radeon_compiler *c,
        struct rc_test_file *test,

Reply via email to