diff options
author | Alexander Shishkin <alexander.shishkin@linux.intel.com> | 2018-10-05 08:43:05 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2018-10-11 06:12:55 -0400 |
commit | ce76d938dd98817f998c905e01fbb99b072c0bf6 (patch) | |
tree | 47f7757c447cf582d7523c7a5e1f467ef28f050c /lib/string.c | |
parent | 6c7e4b6882ad080dad623ba2f4c1a4db578313cb (diff) |
lib: Add memcat_p(): paste 2 pointer arrays together
This adds a helper to paste 2 pointer arrays together, useful for merging
various types of attribute arrays. There are a few places in the kernel
tree where this is open coded, and I just added one more in the STM class.
The naming is inspired by memset_p() and memcat(), and partial credit for
it goes to Andy Shevchenko.
This patch adds the function wrapped in a type-enforcing macro and a test
module.
Signed-off-by: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Tested-by: Mathieu Poirier <mathieu.poirier@linaro.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'lib/string.c')
-rw-r--r-- | lib/string.c | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/string.c b/lib/string.c index 2c0900a5d51a..453f35994eb6 100644 --- a/lib/string.c +++ b/lib/string.c | |||
@@ -27,6 +27,7 @@ | |||
27 | #include <linux/export.h> | 27 | #include <linux/export.h> |
28 | #include <linux/bug.h> | 28 | #include <linux/bug.h> |
29 | #include <linux/errno.h> | 29 | #include <linux/errno.h> |
30 | #include <linux/slab.h> | ||
30 | 31 | ||
31 | #include <asm/byteorder.h> | 32 | #include <asm/byteorder.h> |
32 | #include <asm/word-at-a-time.h> | 33 | #include <asm/word-at-a-time.h> |
@@ -890,6 +891,36 @@ void *memscan(void *addr, int c, size_t size) | |||
890 | EXPORT_SYMBOL(memscan); | 891 | EXPORT_SYMBOL(memscan); |
891 | #endif | 892 | #endif |
892 | 893 | ||
894 | /* | ||
895 | * Merge two NULL-terminated pointer arrays into a newly allocated | ||
896 | * array, which is also NULL-terminated. Nomenclature is inspired by | ||
897 | * memset_p() and memcat() found elsewhere in the kernel source tree. | ||
898 | */ | ||
899 | void **__memcat_p(void **a, void **b) | ||
900 | { | ||
901 | void **p = a, **new; | ||
902 | int nr; | ||
903 | |||
904 | /* count the elements in both arrays */ | ||
905 | for (nr = 0, p = a; *p; nr++, p++) | ||
906 | ; | ||
907 | for (p = b; *p; nr++, p++) | ||
908 | ; | ||
909 | /* one for the NULL-terminator */ | ||
910 | nr++; | ||
911 | |||
912 | new = kmalloc_array(nr, sizeof(void *), GFP_KERNEL); | ||
913 | if (!new) | ||
914 | return NULL; | ||
915 | |||
916 | /* nr -> last index; p points to NULL in b[] */ | ||
917 | for (nr--; nr >= 0; nr--, p = p == b ? &a[nr] : p - 1) | ||
918 | new[nr] = *p; | ||
919 | |||
920 | return new; | ||
921 | } | ||
922 | EXPORT_SYMBOL_GPL(__memcat_p); | ||
923 | |||
893 | #ifndef __HAVE_ARCH_STRSTR | 924 | #ifndef __HAVE_ARCH_STRSTR |
894 | /** | 925 | /** |
895 | * strstr - Find the first substring in a %NUL terminated string | 926 | * strstr - Find the first substring in a %NUL terminated string |