memcat_p.c 753 B

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