target/esirisc: free memory at OpenOCD exit

The target esirisc does not free the allocated memory resources,
causing memory leaks at OpenOCD exit.

Add esirisc_free_reg_cache() and esirisc_deinit_target() and use
them to free all the allocated resources.

Change-Id: I17b8ebff54906fa25a37f2d96c01d010a98cffbd
Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/8094
Tested-by: jenkins
Reviewed-by: Steven Stallion <sstallion@gmail.com>
This commit is contained in:
Antonio Borneo 2024-01-14 17:51:41 +01:00
parent 1b0ffa97ea
commit 9659a9b5e2
1 changed files with 40 additions and 0 deletions

View File

@ -1486,6 +1486,32 @@ static struct reg_cache *esirisc_build_reg_cache(struct target *target)
return cache;
}
static void esirisc_free_reg_cache(struct target *target)
{
struct esirisc_common *esirisc = target_to_esirisc(target);
struct reg_cache *cache = esirisc->reg_cache;
struct reg *reg_list = cache->reg_list;
for (int i = 0; i < esirisc->num_regs; ++i) {
struct reg *reg = reg_list + esirisc_regs[i].number;
free(reg->arch_info);
free(reg->value);
free(reg->reg_data_type);
}
for (size_t i = 0; i < ARRAY_SIZE(esirisc_csrs); ++i) {
struct reg *reg = reg_list + esirisc_csrs[i].number;
free(reg->arch_info);
free(reg->value);
free(reg->reg_data_type);
}
free(reg_list);
free(cache);
}
static int esirisc_identify(struct target *target)
{
struct esirisc_common *esirisc = target_to_esirisc(target);
@ -1584,6 +1610,19 @@ static int esirisc_init_target(struct command_context *cmd_ctx, struct target *t
return ERROR_OK;
}
static void esirisc_deinit_target(struct target *target)
{
struct esirisc_common *esirisc = target_to_esirisc(target);
if (!target_was_examined(target))
return;
esirisc_free_reg_cache(target);
free(esirisc->gdb_arch);
free(esirisc);
}
static int esirisc_examine(struct target *target)
{
struct esirisc_common *esirisc = target_to_esirisc(target);
@ -1822,5 +1861,6 @@ struct target_type esirisc_target = {
.target_create = esirisc_target_create,
.init_target = esirisc_init_target,
.deinit_target = esirisc_deinit_target,
.examine = esirisc_examine,
};