Re: [PATCH v5 4/5] perf metric: Add utilities to work on ids map.

From: Namhyung Kim
Date: Mon Dec 07 2020 - 08:25:06 EST


On Wed, Dec 2, 2020 at 4:40 PM Ian Rogers <irogers@xxxxxxxxxx> wrote:
>
> Add utilities to new/free an ids hashmap, as well as to union. Add
> testing of the union. Unioning hashmaps will be used when parsing the
> metric, if a value is known then the hashmap is unnecessary, otherwise
> we need to union together all the event ids to compute their values for
> reporting.
>
> Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
> ---
[SNIP]

What about adding a comment that it should not access ids1 and ids2
after this function since they can be released?

> +struct hashmap *ids__union(struct hashmap *ids1, struct hashmap *ids2)
> +{
> + size_t bkt;
> + struct hashmap_entry *cur;
> + int ret;
> + struct expr_id_data *old_data = NULL;
> + char *old_key = NULL;
> +
> + if (!ids1)
> + return ids2;
> +
> + if (!ids2)
> + return ids1;
> +
> + if (hashmap__size(ids1) < hashmap__size(ids2)) {
> + struct hashmap *tmp = ids1;
> +
> + ids1 = ids2;
> + ids2 = tmp;
> + }
> + hashmap__for_each_entry(ids2, cur, bkt) {
> + ret = hashmap__set(ids1, cur->key, cur->value,
> + (const void **)&old_key, (void **)&old_data);
> + free(old_key);
> + free(old_data);
> +
> + if (ret)
> + break;
> + }
> + hashmap__free(ids2);
> + return ids1;
> +}