aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-12-02 03:27:42 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-12-02 03:27:42 +0000
commit4c139229611631a3497c224a61107b6b6918e3bf (patch)
tree122c8bb3315a9d0ecce0963c12209e70bb670aa6
parente2532ab5f2446ec736b10b24f57a36456deb197f (diff)
downloadbusybox-w32-4c139229611631a3497c224a61107b6b6918e3bf.tar.gz
busybox-w32-4c139229611631a3497c224a61107b6b6918e3bf.tar.bz2
busybox-w32-4c139229611631a3497c224a61107b6b6918e3bf.zip
Introduce FEATURE_COPYBUF_KB.
People who want smaller stack at any cost may use it to reduce cp's stack usage (FEATURE_COPYBUF_KB=1). Desktop people may get faster copy of big files (FEATURE_COPYBUF_KB=32 is ~30% faster than 4kb)
-rw-r--r--libbb/Config.in10
-rw-r--r--libbb/copyfd.c32
2 files changed, 34 insertions, 8 deletions
diff --git a/libbb/Config.in b/libbb/Config.in
index 5055015cf..6bbeffb43 100644
--- a/libbb/Config.in
+++ b/libbb/Config.in
@@ -110,6 +110,16 @@ config FEATURE_EDITING_FANCY_PROMPT
110 Setting this option allows for prompts to use things like \w and 110 Setting this option allows for prompts to use things like \w and
111 \$ and escape codes. 111 \$ and escape codes.
112 112
113config FEATURE_COPYBUF_KB
114 int "Copy buffer size, in kilobytes"
115 range 1 1024
116 default 4
117 help
118 Size of buffer used by cp, mv, install etc.
119 Buffers which are 4 kb or less will be allocated on stack.
120 Bigger buffers will be allocated with mmap, with fallback to 4 kb
121 stack buffer if mmap fails.
122
113config MONOTONIC_SYSCALL 123config MONOTONIC_SYSCALL
114 bool "Use clock_gettime(CLOCK_MONOTONIC) syscall" 124 bool "Use clock_gettime(CLOCK_MONOTONIC) syscall"
115 default y 125 default y
diff --git a/libbb/copyfd.c b/libbb/copyfd.c
index 3255e424a..ed383ae28 100644
--- a/libbb/copyfd.c
+++ b/libbb/copyfd.c
@@ -9,31 +9,42 @@
9 9
10#include "libbb.h" 10#include "libbb.h"
11 11
12#if BUFSIZ < 4096
13#undef BUFSIZ
14#define BUFSIZ 4096
15#endif
16
17/* Used by NOFORK applets (e.g. cat) - must not use xmalloc */ 12/* Used by NOFORK applets (e.g. cat) - must not use xmalloc */
18 13
19static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size) 14static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
20{ 15{
21 int status = -1; 16 int status = -1;
22 off_t total = 0; 17 off_t total = 0;
23 char buffer[BUFSIZ]; 18#if CONFIG_FEATURE_COPYBUF_KB <= 4
19 char buffer[CONFIG_FEATURE_COPYBUF_KB * 1024];
20 enum { buffer_size = sizeof(buffer) };
21#else
22 char *buffer;
23 int buffer_size;
24
25 buffer = mmap(NULL, CONFIG_FEATURE_COPYBUF_KB * 1024,
26 PROT_READ | PROT_WRITE,
27 MAP_PRIVATE | MAP_ANON,
28 /* ignored: */ -1, 0);
29 buffer_size = CONFIG_FEATURE_COPYBUF_KB * 1024;
30 if (buffer == MAP_FAILED) {
31 buffer = alloca(4 * 1024);
32 buffer_size = 4 * 1024;
33 }
34#endif
24 35
25 if (src_fd < 0) 36 if (src_fd < 0)
26 goto out; 37 goto out;
27 38
28 if (!size) { 39 if (!size) {
29 size = BUFSIZ; 40 size = buffer_size;
30 status = 1; /* copy until eof */ 41 status = 1; /* copy until eof */
31 } 42 }
32 43
33 while (1) { 44 while (1) {
34 ssize_t rd; 45 ssize_t rd;
35 46
36 rd = safe_read(src_fd, buffer, size > BUFSIZ ? BUFSIZ : size); 47 rd = safe_read(src_fd, buffer, size > buffer_size ? buffer_size : size);
37 48
38 if (!rd) { /* eof - all done */ 49 if (!rd) { /* eof - all done */
39 status = 0; 50 status = 0;
@@ -62,6 +73,11 @@ static off_t bb_full_fd_action(int src_fd, int dst_fd, off_t size)
62 } 73 }
63 } 74 }
64 out: 75 out:
76
77#if CONFIG_FEATURE_COPYBUF_KB > 4
78 if (buffer_size != 4 * 1024)
79 munmap(buffer, buffer_size);
80#endif
65 return status ? -1 : total; 81 return status ? -1 : total;
66} 82}
67 83