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