Skip to content

Commit 4ddd327

Browse files
committed
tools: Adopt memdup() from tools/perf, moving it to tools/lib/string.c
That will contain more string functions with counterparts, sometimes verbatim copies, in the kernel. Acked-by: Wang Nan <wangnan0@huawei.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: David Ahern <dsahern@gmail.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Link: http://lkml.kernel.org/n/tip-rah6g97kn21vfgmlramorz6o@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 9a13c65 commit 4ddd327

6 files changed

Lines changed: 36 additions & 19 deletions

File tree

‎tools/include/linux/string.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _TOOLS_LINUX_STRING_H_
2+
#define _TOOLS_LINUX_STRING_H_
3+
4+
5+
#include <linux/types.h> /* for size_t */
6+
7+
void *memdup(const void *src, size_t len);
8+
9+
#endif /* _LINUX_STRING_H_ */

‎tools/lib/string.c‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <linux/string.h>
4+
5+
/**
6+
* memdup - duplicate region of memory
7+
*
8+
* @src: memory region to duplicate
9+
* @len: memory region length
10+
*/
11+
void *memdup(const void *src, size_t len)
12+
{
13+
void *p = malloc(len);
14+
15+
if (p)
16+
memcpy(p, src, len);
17+
18+
return p;
19+
}

‎tools/perf/MANIFEST‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ tools/lib/api
2222
tools/lib/bpf
2323
tools/lib/hweight.c
2424
tools/lib/rbtree.c
25+
tools/lib/string.c
2526
tools/lib/symbol/kallsyms.c
2627
tools/lib/symbol/kallsyms.h
2728
tools/lib/util/find_next_bit.c
@@ -50,6 +51,7 @@ tools/include/linux/log2.h
5051
tools/include/linux/poison.h
5152
tools/include/linux/rbtree.h
5253
tools/include/linux/rbtree_augmented.h
54+
tools/include/linux/string.h
5355
tools/include/linux/types.h
5456
tools/include/linux/err.h
5557
include/asm-generic/bitops/arch_hweight.h

‎tools/perf/util/Build‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ libperf-y += parse-events.o
2121
libperf-y += perf_regs.o
2222
libperf-y += path.o
2323
libperf-y += rbtree.o
24+
libperf-y += libstring.o
2425
libperf-y += bitmap.o
2526
libperf-y += hweight.o
2627
libperf-y += run-command.o
@@ -138,6 +139,7 @@ $(OUTPUT)util/pmu.o: $(OUTPUT)util/pmu-flex.c $(OUTPUT)util/pmu-bison.c
138139

139140
CFLAGS_find_next_bit.o += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
140141
CFLAGS_rbtree.o += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
142+
CFLAGS_libstring.o += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
141143
CFLAGS_hweight.o += -Wno-unused-parameter -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
142144
CFLAGS_parse-events.o += -Wno-redundant-decls
143145

@@ -153,6 +155,10 @@ $(OUTPUT)util/rbtree.o: ../lib/rbtree.c FORCE
153155
$(call rule_mkdir)
154156
$(call if_changed_dep,cc_o_c)
155157

158+
$(OUTPUT)util/libstring.o: ../lib/string.c FORCE
159+
$(call rule_mkdir)
160+
$(call if_changed_dep,cc_o_c)
161+
156162
$(OUTPUT)util/hweight.o: ../lib/hweight.c FORCE
157163
$(call rule_mkdir)
158164
$(call if_changed_dep,cc_o_c)

‎tools/perf/util/include/linux/string.h‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎tools/perf/util/string.c‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -342,22 +342,6 @@ char *rtrim(char *s)
342342
return s;
343343
}
344344

345-
/**
346-
* memdup - duplicate region of memory
347-
* @src: memory region to duplicate
348-
* @len: memory region length
349-
*/
350-
void *memdup(const void *src, size_t len)
351-
{
352-
void *p;
353-
354-
p = malloc(len);
355-
if (p)
356-
memcpy(p, src, len);
357-
358-
return p;
359-
}
360-
361345
char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
362346
{
363347
/*

0 commit comments

Comments
 (0)