Skip to content

Commit

Permalink
target/esirisc: free memory at OpenOCD exit
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
borneoa committed Jan 28, 2024
1 parent 1b0ffa9 commit 9659a9b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/target/esirisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
};

0 comments on commit 9659a9b

Please sign in to comment.