xref: /openbmc/linux/lib/memcat_p.c (revision 93048c09)
193048c09SAlexander Shishkin // SPDX-License-Identifier: GPL-2.0
293048c09SAlexander Shishkin 
393048c09SAlexander Shishkin #include <linux/slab.h>
493048c09SAlexander Shishkin 
593048c09SAlexander Shishkin /*
693048c09SAlexander Shishkin  * Merge two NULL-terminated pointer arrays into a newly allocated
793048c09SAlexander Shishkin  * array, which is also NULL-terminated. Nomenclature is inspired by
893048c09SAlexander Shishkin  * memset_p() and memcat() found elsewhere in the kernel source tree.
993048c09SAlexander Shishkin  */
__memcat_p(void ** a,void ** b)1093048c09SAlexander Shishkin void **__memcat_p(void **a, void **b)
1193048c09SAlexander Shishkin {
1293048c09SAlexander Shishkin 	void **p = a, **new;
1393048c09SAlexander Shishkin 	int nr;
1493048c09SAlexander Shishkin 
1593048c09SAlexander Shishkin 	/* count the elements in both arrays */
1693048c09SAlexander Shishkin 	for (nr = 0, p = a; *p; nr++, p++)
1793048c09SAlexander Shishkin 		;
1893048c09SAlexander Shishkin 	for (p = b; *p; nr++, p++)
1993048c09SAlexander Shishkin 		;
2093048c09SAlexander Shishkin 	/* one for the NULL-terminator */
2193048c09SAlexander Shishkin 	nr++;
2293048c09SAlexander Shishkin 
2393048c09SAlexander Shishkin 	new = kmalloc_array(nr, sizeof(void *), GFP_KERNEL);
2493048c09SAlexander Shishkin 	if (!new)
2593048c09SAlexander Shishkin 		return NULL;
2693048c09SAlexander Shishkin 
2793048c09SAlexander Shishkin 	/* nr -> last index; p points to NULL in b[] */
2893048c09SAlexander Shishkin 	for (nr--; nr >= 0; nr--, p = p == b ? &a[nr] : p - 1)
2993048c09SAlexander Shishkin 		new[nr] = *p;
3093048c09SAlexander Shishkin 
3193048c09SAlexander Shishkin 	return new;
3293048c09SAlexander Shishkin }
3393048c09SAlexander Shishkin EXPORT_SYMBOL_GPL(__memcat_p);
3493048c09SAlexander Shishkin 
35