Re: [PATCH] devres: provide API devm_kstrndup

Andy Shevchenko
2023-05-04T14:38:38Z

On Thu, May 04, 2023 at 03:57:54PM +0800, Peng Fan (OSS) wrote:
> From: Peng Fan <peng.fan@nxp.com>
>
> 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