diff options
author | Ron Yorston <rmy@frippery.org> | 2015-07-19 23:05:20 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-07-19 23:05:20 +0200 |
commit | d840c5d139cfa50fbe4f6f67c178b0edf0c690c8 (patch) | |
tree | e75010ca3ce7769f53a6170ebe940f37c6a94dc1 /libbb | |
parent | 78cfa00154dca18a1326d2064121bf65cd081781 (diff) | |
download | busybox-w32-d840c5d139cfa50fbe4f6f67c178b0edf0c690c8.tar.gz busybox-w32-d840c5d139cfa50fbe4f6f67c178b0edf0c690c8.tar.bz2 busybox-w32-d840c5d139cfa50fbe4f6f67c178b0edf0c690c8.zip |
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 <rmy@frippery.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/procps.c | 3 | ||||
-rw-r--r-- | libbb/xfuncs_printf.c | 5 |
2 files changed, 6 insertions, 2 deletions
diff --git a/libbb/procps.c b/libbb/procps.c index 3d335b37b..71ad071e6 100644 --- a/libbb/procps.c +++ b/libbb/procps.c | |||
@@ -554,8 +554,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags) | |||
554 | break; | 554 | break; |
555 | if (flags & PSSCAN_ARGVN) { | 555 | if (flags & PSSCAN_ARGVN) { |
556 | sp->argv_len = n; | 556 | sp->argv_len = n; |
557 | sp->argv0 = xmalloc(n + 1); | 557 | sp->argv0 = xmemdup(buf, n + 1); |
558 | memcpy(sp->argv0, buf, n + 1); | ||
559 | /* sp->argv0[n] = '\0'; - buf has it */ | 558 | /* sp->argv0[n] = '\0'; - buf has it */ |
560 | } else { | 559 | } else { |
561 | sp->argv_len = 0; | 560 | sp->argv_len = 0; |
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) | |||
112 | return memcpy(t, s, n); | 112 | return memcpy(t, s, n); |
113 | } | 113 | } |
114 | 114 | ||
115 | void* FAST_FUNC xmemdup(const void *s, int n) | ||
116 | { | ||
117 | return memcpy(xmalloc(n), s, n); | ||
118 | } | ||
119 | |||
115 | // Die if we can't open a file and return a FILE* to it. | 120 | // Die if we can't open a file and return a FILE* to it. |
116 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. | 121 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. |
117 | FILE* FAST_FUNC xfopen(const char *path, const char *mode) | 122 | FILE* FAST_FUNC xfopen(const char *path, const char *mode) |