Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/slab.h>
0004 
0005 /*
0006  * Merge two NULL-terminated pointer arrays into a newly allocated
0007  * array, which is also NULL-terminated. Nomenclature is inspired by
0008  * memset_p() and memcat() found elsewhere in the kernel source tree.
0009  */
0010 void **__memcat_p(void **a, void **b)
0011 {
0012     void **p = a, **new;
0013     int nr;
0014 
0015     /* count the elements in both arrays */
0016     for (nr = 0, p = a; *p; nr++, p++)
0017         ;
0018     for (p = b; *p; nr++, p++)
0019         ;
0020     /* one for the NULL-terminator */
0021     nr++;
0022 
0023     new = kmalloc_array(nr, sizeof(void *), GFP_KERNEL);
0024     if (!new)
0025         return NULL;
0026 
0027     /* nr -> last index; p points to NULL in b[] */
0028     for (nr--; nr >= 0; nr--, p = p == b ? &a[nr] : p - 1)
0029         new[nr] = *p;
0030 
0031     return new;
0032 }
0033 EXPORT_SYMBOL_GPL(__memcat_p);
0034