Re: [PATCH] devres: provide API devm_kstrndup

From: Andy Shevchenko
Date: Thu May 04 2023 - 10:38:39 EST


On Thu, May 04, 2023 at 03:57:54PM +0800, Peng Fan (OSS) wrote:
> From: Peng Fan <peng.fan@xxxxxxx>
>
> This patch introduces devm_kstrndup API so that the
> device's driver can allocate memory and copy string.

...

> +char *devm_kstrndup(struct device *dev, const char *s, size_t max, gfp_t gfp)
> +{
> + size_t len;
> + char *buf;
> +
> + if (!s)
> + return NULL;
> +
> + len = strnlen(s, max);

> + buf = devm_kmalloc(dev, len + 1, gfp);
> + if (buf) {
> + memcpy(buf, s, len);

besides this to be open coded devm_kmemdup()

> + buf[len] = '\0';
> + }
> + return buf;
> +}

you can always use devm_add_action_or_reset() for a single user before
we introduce a new wide API.

--
With Best Regards,
Andy Shevchenko