Re: [PATCH v5 1/5] perf metric: Restructure struct expr_parse_ctx.

From: Namhyung Kim
Date: Mon Dec 07 2020 - 08:21:19 EST


Hi Ian,

On Wed, Dec 2, 2020 at 4:40 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
>
> A later change to parsing the ids out (in expr__find_other) will
> potentially drop hashmaps and so it is more convenient to move
> expr_parse_ctx to have a hashmap pointer rather than a struct value. As
> this pointer must be freed, rather than just going out of scope,
> add expr__ctx_new and expr__ctx_free to manage expr_parse_ctx memory.
> Adjust use of struct expr_parse_ctx accordingly.
>
> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
> ---
[SNIP]
> -void expr__ctx_init(struct expr_parse_ctx *ctx)
> +struct expr_parse_ctx *expr__ctx_new(void)
> {
> - hashmap__init(&ctx->ids, key_hash, key_equal, NULL);
> + struct expr_parse_ctx *ctx;
> +
> + ctx = malloc(sizeof(struct expr_parse_ctx));
> + if (!ctx)
> + return NULL;
> +
> + ctx->ids = hashmap__new(key_hash, key_equal, NULL);
> + ctx->parent = NULL;
> + return ctx;
> }
>
> void expr__ctx_clear(struct expr_parse_ctx *ctx)
> @@ -221,11 +229,23 @@ void expr__ctx_clear(struct expr_parse_ctx *ctx)
> struct hashmap_entry *cur;
> size_t bkt;
>
> - hashmap__for_each_entry((&ctx->ids), cur, bkt) {
> + hashmap__for_each_entry(ctx->ids, cur, bkt) {
> + free((char *)cur->key);
> + free(cur->value);
> + }
> + hashmap__clear(ctx->ids);
> +}
> +
> +void expr__ctx_free(struct expr_parse_ctx *ctx)
> +{
> + struct hashmap_entry *cur;
> + size_t bkt;
> +
> + hashmap__for_each_entry(ctx->ids, cur, bkt) {
> free((char *)cur->key);
> free(cur->value);
> }
> - hashmap__clear(&ctx->ids);
> + hashmap__free(ctx->ids);
> }

I think this function should free the ctx itself.

Thanks,
Namhyung