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