linux/lib/memcat_p.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2
   3#include <linux/slab.h>
   4
   5/*
   6 * Merge two NULL-terminated pointer arrays into a newly allocated
   7 * array, which is also NULL-terminated. Nomenclature is inspired by
   8 * memset_p() and memcat() found elsewhere in the kernel source tree.
   9 */
  10void **__memcat_p(void **a, void **b)
  11{
  12        void **p = a, **new;
  13        int nr;
  14
  15        /* count the elements in both arrays */
  16        for (nr = 0, p = a; *p; nr++, p++)
  17                ;
  18        for (p = b; *p; nr++, p++)
  19                ;
  20        /* one for the NULL-terminator */
  21        nr++;
  22
  23        new = kmalloc_array(nr, sizeof(void *), GFP_KERNEL);
  24        if (!new)
  25                return NULL;
  26
  27        /* nr -> last index; p points to NULL in b[] */
  28        for (nr--; nr >= 0; nr--, p = p == b ? &a[nr] : p - 1)
  29                new[nr] = *p;
  30
  31        return new;
  32}
  33EXPORT_SYMBOL_GPL(__memcat_p);
  34
  35