From d840c5d139cfa50fbe4f6f67c178b0edf0c690c8 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sun, 19 Jul 2015 23:05:20 +0200 Subject: libbb: add a function to make a copy of a region of memory Introduce a library routine to package the idiom: p = xmalloc(b, n); memcpy(p, b, n); and use it where possible. The example in traceroute used xzalloc but it didn't need to. function old new delta xmemdup - 32 +32 last_main 834 826 -8 make_device 2321 2311 -10 common_traceroute_main 3698 3685 -13 readtoken1 3182 3168 -14 procps_scan 1222 1206 -16 forkchild 655 638 -17 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 0/6 up/down: 32/-78) Total: -46 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- libbb/xfuncs_printf.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'libbb/xfuncs_printf.c') diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index e4ac6a002..73488908d 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -112,6 +112,11 @@ char* FAST_FUNC xstrndup(const char *s, int n) return memcpy(t, s, n); } +void* FAST_FUNC xmemdup(const void *s, int n) +{ + return memcpy(xmalloc(n), s, n); +} + // Die if we can't open a file and return a FILE* to it. // Notice we haven't got xfread(), This is for use with fscanf() and friends. FILE* FAST_FUNC xfopen(const char *path, const char *mode) -- cgit v1.2.3