Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / lib / memcat_p.c
blobb810fbc66962f79d01284c9b90ac4d050678f7bc
1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/slab.h>
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 */
10 void **__memcat_p(void **a, void **b)
12 void **p = a, **new;
13 int nr;
15 /* count the elements in both arrays */
16 for (nr = 0, p = a; *p; nr++, p++)
18 for (p = b; *p; nr++, p++)
20 /* one for the NULL-terminator */
21 nr++;
23 new = kmalloc_array(nr, sizeof(void *), GFP_KERNEL);
24 if (!new)
25 return NULL;
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;
31 return new;
33 EXPORT_SYMBOL_GPL(__memcat_p);