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 --- networking/traceroute.c | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'networking/traceroute.c') diff --git a/networking/traceroute.c b/networking/traceroute.c index 97a7a19e0..12ba614e8 100644 --- a/networking/traceroute.c +++ b/networking/traceroute.c @@ -387,15 +387,6 @@ struct globals { #define outudp ((struct udphdr *)(outip + 1)) -/* libbb candidate? tftp uses this idiom too */ -static len_and_sockaddr* dup_sockaddr(const len_and_sockaddr *lsa) -{ - len_and_sockaddr *new_lsa = xzalloc(LSA_LEN_SIZE + lsa->len); - memcpy(new_lsa, lsa, LSA_LEN_SIZE + lsa->len); - return new_lsa; -} - - static int wait_for_reply(len_and_sockaddr *from_lsa, struct sockaddr *to, unsigned *timestamp_us, int *left_ms) { @@ -1074,7 +1065,7 @@ common_traceroute_main(int op, char **argv) printf(" from %s", source); printf(", %d hops max, %d byte packets\n", max_ttl, packlen); - from_lsa = dup_sockaddr(dest_lsa); + from_lsa = xmemdup(dest_lsa, LSA_LEN_SIZE + dest_lsa->len); lastaddr = xzalloc(dest_lsa->len); to = xzalloc(dest_lsa->len); seq = 0; -- cgit v1.2.3