[waffle] [PATCH 07/11] core: Add func wcore_attrib_list_copy()

Emil Velikov emil.l.velikov at gmail.com
Sun Dec 21 07:00:05 PST 2014


On 16 December 2014 at 08:18, Chad Versace <chad.versace at linux.intel.com> wrote:
> This is useful for making a writable copy of a read-only attribute list.
>
> Signed-off-by: Chad Versace <chad.versace at linux.intel.com>
> ---
>  src/waffle/core/wcore_attrib_list.c | 19 +++++++++++++++++++
>  src/waffle/core/wcore_attrib_list.h |  3 +++
>  2 files changed, 22 insertions(+)
>
> diff --git a/src/waffle/core/wcore_attrib_list.c b/src/waffle/core/wcore_attrib_list.c
> index a7f087d..94013b9 100644
> --- a/src/waffle/core/wcore_attrib_list.c
> +++ b/src/waffle/core/wcore_attrib_list.c
> @@ -28,6 +28,7 @@
>  #include <stdbool.h>
>  #include <stdint.h>
>  #include <stddef.h>
> +#include <string.h>
>
>  size_t
>  wcore_attrib_list_length(const intptr_t attrib_list[])
> @@ -43,6 +44,24 @@ wcore_attrib_list_length(const intptr_t attrib_list[])
>      return (i - attrib_list) / 2;
>  }
>
> +intptr_t*
> +wcore_attrib_list_copy(const intptr_t attrib_list[])
> +{
> +    intptr_t *copy;
> +
> +    if (attrib_list) {
> +        size_t length = wcore_attrib_list_length(attrib_list);
> +        intptr_t size = (2 * length + 1) * sizeof(intptr_t);
> +        copy = malloc(size);
> +        memcpy(copy, attrib_list, size);
> +    } else {
> +        copy = malloc(sizeof(intptr_t));
> +        copy[0] = 0;
> +    }
We might want to add error checking after the malloc - to silence
static analysers at the very least :-)

-Emil


More information about the waffle mailing list