aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-01-25 23:49:09 +0000
committerEric Andersen <andersen@codepoet.org>2001-01-25 23:49:09 +0000
commitd35c21587a4139031c077fd122252217a4713681 (patch)
tree7bd14fd247492c00f3d38ae1dbd7727e03fda9ad
parentffde8673fe8b2c32076aa3e01eab1fefc5f08e86 (diff)
downloadbusybox-w32-d35c21587a4139031c077fd122252217a4713681.tar.gz
busybox-w32-d35c21587a4139031c077fd122252217a4713681.tar.bz2
busybox-w32-d35c21587a4139031c077fd122252217a4713681.zip
Commit Larry Doolittle's buffers-on-stack/buffers-via-malloc patch.
-Erik
-rw-r--r--Config.h5
-rw-r--r--archival/gunzip.c4
-rw-r--r--busybox.h8
-rw-r--r--coreutils/tr.c9
-rw-r--r--cp_mv.c4
-rw-r--r--docs/style-guide.txt18
-rw-r--r--gunzip.c4
-rw-r--r--include/busybox.h8
-rw-r--r--rpmunpack.c2
-rw-r--r--sysklogd/syslogd.c4
-rw-r--r--syslogd.c4
-rw-r--r--tr.c9
12 files changed, 51 insertions, 28 deletions
diff --git a/Config.h b/Config.h
index 667612cb5..a2e2d0469 100644
--- a/Config.h
+++ b/Config.h
@@ -129,6 +129,11 @@
129// pretty/useful). 129// pretty/useful).
130// 130//
131// 131//
132// BusyBox will, by default, malloc space for its buffers. This costs code
133// size for the call to xmalloc. You can use the following feature to have
134// them put on the stack. For some very small machines with limited stack
135// space, this can be deadly. For most folks, this works just fine...
136//#define BB_FEATURE_BUFFERS_GO_ON_STACK
132// 137//
133// Turn this on to use Erik's very cool devps, and devmtab kernel drivers, 138// Turn this on to use Erik's very cool devps, and devmtab kernel drivers,
134// thereby eliminating the need for the /proc filesystem and thereby saving 139// thereby eliminating the need for the /proc filesystem and thereby saving
diff --git a/archival/gunzip.c b/archival/gunzip.c
index 194921682..09571f91c 100644
--- a/archival/gunzip.c
+++ b/archival/gunzip.c
@@ -1222,8 +1222,8 @@ int gunzip_main(int argc, char **argv)
1222 int force = 0; 1222 int force = 0;
1223 struct stat statBuf; 1223 struct stat statBuf;
1224 char *delFileName; 1224 char *delFileName;
1225 char ifname[MAX_PATH_LEN + 1]; /* input file name */ 1225 RESERVE_BB_BUFFER(ifname, MAX_PATH_LEN+1); /* input file name */
1226 char ofname[MAX_PATH_LEN + 1]; /* output file name */ 1226 RESERVE_BB_BUFFER(ofname, MAX_PATH_LEN+1); /* output file name */
1227 1227
1228 method = DEFLATED; /* default compression method */ 1228 method = DEFLATED; /* default compression method */
1229 exit_code = OK; /* let's go out on a limb and assume everything will run fine (wink wink) */ 1229 exit_code = OK; /* let's go out on a limb and assume everything will run fine (wink wink) */
diff --git a/busybox.h b/busybox.h
index 018c636cf..be6c6f305 100644
--- a/busybox.h
+++ b/busybox.h
@@ -266,4 +266,12 @@ char *format(unsigned long val, unsigned long hr);
266#define GIGABYTE (MEGABYTE*1024) 266#define GIGABYTE (MEGABYTE*1024)
267#endif 267#endif
268 268
269#ifdef BB_FEATURE_BUFFERS_GO_ON_STACK
270#define RESERVE_BB_BUFFER(buffer,len) char buffer[len]
271#define RESERVE_BB_UBUFFER(buffer,len) unsigned char buffer[len]
272#else
273#define RESERVE_BB_BUFFER(buffer,len) char *buffer=xmalloc(len)
274#define RESERVE_BB_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
275#endif
276
269#endif /* _BB_INTERNAL_H_ */ 277#endif /* _BB_INTERNAL_H_ */
diff --git a/coreutils/tr.c b/coreutils/tr.c
index d21e672fe..15e3709bb 100644
--- a/coreutils/tr.c
+++ b/coreutils/tr.c
@@ -144,10 +144,11 @@ extern int tr_main(int argc, char **argv)
144 int output_length=0, input_length; 144 int output_length=0, input_length;
145 int index = 1; 145 int index = 1;
146 int i; 146 int i;
147 /* set up big arrays here (better than making a bunch of static arrays up top) */ 147 RESERVE_BB_BUFFER(output, BUFSIZ);
148 unsigned char output[BUFSIZ], input[BUFSIZ]; 148 RESERVE_BB_BUFFER(input, BUFSIZ);
149 unsigned char vector[ASCII + 1]; 149 RESERVE_BB_UBUFFER(vector, ASCII+1);
150 char invec[ASCII + 1], outvec[ASCII + 1]; 150 RESERVE_BB_BUFFER(invec, ASCII+1);
151 RESERVE_BB_BUFFER(outvec, ASCII+1);
151 152
152 /* ... but make them available globally */ 153 /* ... but make them available globally */
153 poutput = output; 154 poutput = output;
diff --git a/cp_mv.c b/cp_mv.c
index 55483505f..9bcac02d4 100644
--- a/cp_mv.c
+++ b/cp_mv.c
@@ -175,8 +175,8 @@ extern int cp_mv_main(int argc, char **argv)
175{ 175{
176 volatile int i; 176 volatile int i;
177 int c; 177 int c;
178 char baseDestName[BUFSIZ + 1]; /* not declared globally == less bss used */ 178 RESERVE_BB_BUFFER(baseDestName,BUFSIZ + 1);
179 pBaseDestName = baseDestName; /* but available globally */ 179 pBaseDestName = baseDestName; /* available globally */
180 180
181 if (*applet_name == 'c' && *(applet_name + 1) == 'p') 181 if (*applet_name == 'c' && *(applet_name + 1) == 'p')
182 dz_i = is_cp; 182 dz_i = is_cp;
diff --git a/docs/style-guide.txt b/docs/style-guide.txt
index 9a3b10207..1a04e4474 100644
--- a/docs/style-guide.txt
+++ b/docs/style-guide.txt
@@ -402,7 +402,7 @@ The problem with these is that any time any busybox app is run, you pay a
402memory penalty for this buffer, even if the applet that uses said buffer is 402memory penalty for this buffer, even if the applet that uses said buffer is
403not run. This can be fixed, thusly: 403not run. This can be fixed, thusly:
404 404
405 static char *buffer 405 static char *buffer;
406 ... 406 ...
407 other_func() 407 other_func()
408 { 408 {
@@ -418,7 +418,7 @@ mallocing the buffers (and thus growing the text size), buffers can be
418declared on the stack in the *_main() function and made available globally by 418declared on the stack in the *_main() function and made available globally by
419assigning them to a global pointer thusly: 419assigning them to a global pointer thusly:
420 420
421 static char *pbuffer 421 static char *pbuffer;
422 ... 422 ...
423 other_func() 423 other_func()
424 { 424 {
@@ -430,13 +430,13 @@ assigning them to a global pointer thusly:
430 pbuffer = buffer; /* but available globally */ 430 pbuffer = buffer; /* but available globally */
431 ... 431 ...
432 432
433Thus: 433This last approach has some advantages (low code size, space not used until
434 - global static buffers are eliminated 434it's needed), but can be a problem in some low resource machines that have
435 - we don't grow the text segment as much because no malloc() call is made; 435very limited stack space (e.g., uCLinux). busybox.h declares a macro that
436 memory is automatically allocated on the stack when execution context 436implements compile-time selection between xmalloc() and stack creation, so
437 enters the function. (We still grow text a little bit because of the 437you can code the line in question as
438 assignment, but that's cheap compared to a function call.) 438 RESERVE_BB_BUFFER(buffer, BUFSIZ);
439 - the buffer is still available globally via the pointer 439and the right thing will happen, based on the customer's configuration.
440 440
441 441
442 442
diff --git a/gunzip.c b/gunzip.c
index 194921682..09571f91c 100644
--- a/gunzip.c
+++ b/gunzip.c
@@ -1222,8 +1222,8 @@ int gunzip_main(int argc, char **argv)
1222 int force = 0; 1222 int force = 0;
1223 struct stat statBuf; 1223 struct stat statBuf;
1224 char *delFileName; 1224 char *delFileName;
1225 char ifname[MAX_PATH_LEN + 1]; /* input file name */ 1225 RESERVE_BB_BUFFER(ifname, MAX_PATH_LEN+1); /* input file name */
1226 char ofname[MAX_PATH_LEN + 1]; /* output file name */ 1226 RESERVE_BB_BUFFER(ofname, MAX_PATH_LEN+1); /* output file name */
1227 1227
1228 method = DEFLATED; /* default compression method */ 1228 method = DEFLATED; /* default compression method */
1229 exit_code = OK; /* let's go out on a limb and assume everything will run fine (wink wink) */ 1229 exit_code = OK; /* let's go out on a limb and assume everything will run fine (wink wink) */
diff --git a/include/busybox.h b/include/busybox.h
index 018c636cf..be6c6f305 100644
--- a/include/busybox.h
+++ b/include/busybox.h
@@ -266,4 +266,12 @@ char *format(unsigned long val, unsigned long hr);
266#define GIGABYTE (MEGABYTE*1024) 266#define GIGABYTE (MEGABYTE*1024)
267#endif 267#endif
268 268
269#ifdef BB_FEATURE_BUFFERS_GO_ON_STACK
270#define RESERVE_BB_BUFFER(buffer,len) char buffer[len]
271#define RESERVE_BB_UBUFFER(buffer,len) unsigned char buffer[len]
272#else
273#define RESERVE_BB_BUFFER(buffer,len) char *buffer=xmalloc(len)
274#define RESERVE_BB_UBUFFER(buffer,len) unsigned char *buffer=xmalloc(len)
275#endif
276
269#endif /* _BB_INTERNAL_H_ */ 277#endif /* _BB_INTERNAL_H_ */
diff --git a/rpmunpack.c b/rpmunpack.c
index 512d63839..0f36077c1 100644
--- a/rpmunpack.c
+++ b/rpmunpack.c
@@ -51,7 +51,7 @@ static void myread(int num, char *buffer)
51int rpmunpack_main(int argc, char **argv) 51int rpmunpack_main(int argc, char **argv)
52{ 52{
53 int len, status = 0; 53 int len, status = 0;
54 char buffer[BUFSIZ]; 54 RESERVE_BB_BUFFER(buffer, BUFSIZ);
55 55
56 /* Get our own program name */ 56 /* Get our own program name */
57 if ((progname = strrchr(argv[0], '/')) == NULL) 57 if ((progname = strrchr(argv[0], '/')) == NULL)
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 114516e2f..972fda15f 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -209,7 +209,7 @@ static void domark(int sig)
209static const int BUFSIZE = 1023; 209static const int BUFSIZE = 1023;
210static int serveConnection (int conn) 210static int serveConnection (int conn)
211{ 211{
212 char buf[ BUFSIZE + 1 ]; 212 RESERVE_BB_BUFFER(buf, BUFSIZE + 1);
213 int n_read; 213 int n_read;
214 214
215 while ((n_read = read (conn, buf, BUFSIZE )) > 0) { 215 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
@@ -296,7 +296,7 @@ static void doSyslogd (void)
296 int sock_fd; 296 int sock_fd;
297 fd_set fds; 297 fd_set fds;
298 298
299 char lfile[BUFSIZ]; 299 RESERVE_BB_BUFFER(lfile, BUFSIZ);
300 300
301 /* Set up signal handlers. */ 301 /* Set up signal handlers. */
302 signal (SIGINT, quit_signal); 302 signal (SIGINT, quit_signal);
diff --git a/syslogd.c b/syslogd.c
index 114516e2f..972fda15f 100644
--- a/syslogd.c
+++ b/syslogd.c
@@ -209,7 +209,7 @@ static void domark(int sig)
209static const int BUFSIZE = 1023; 209static const int BUFSIZE = 1023;
210static int serveConnection (int conn) 210static int serveConnection (int conn)
211{ 211{
212 char buf[ BUFSIZE + 1 ]; 212 RESERVE_BB_BUFFER(buf, BUFSIZE + 1);
213 int n_read; 213 int n_read;
214 214
215 while ((n_read = read (conn, buf, BUFSIZE )) > 0) { 215 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
@@ -296,7 +296,7 @@ static void doSyslogd (void)
296 int sock_fd; 296 int sock_fd;
297 fd_set fds; 297 fd_set fds;
298 298
299 char lfile[BUFSIZ]; 299 RESERVE_BB_BUFFER(lfile, BUFSIZ);
300 300
301 /* Set up signal handlers. */ 301 /* Set up signal handlers. */
302 signal (SIGINT, quit_signal); 302 signal (SIGINT, quit_signal);
diff --git a/tr.c b/tr.c
index d21e672fe..15e3709bb 100644
--- a/tr.c
+++ b/tr.c
@@ -144,10 +144,11 @@ extern int tr_main(int argc, char **argv)
144 int output_length=0, input_length; 144 int output_length=0, input_length;
145 int index = 1; 145 int index = 1;
146 int i; 146 int i;
147 /* set up big arrays here (better than making a bunch of static arrays up top) */ 147 RESERVE_BB_BUFFER(output, BUFSIZ);
148 unsigned char output[BUFSIZ], input[BUFSIZ]; 148 RESERVE_BB_BUFFER(input, BUFSIZ);
149 unsigned char vector[ASCII + 1]; 149 RESERVE_BB_UBUFFER(vector, ASCII+1);
150 char invec[ASCII + 1], outvec[ASCII + 1]; 150 RESERVE_BB_BUFFER(invec, ASCII+1);
151 RESERVE_BB_BUFFER(outvec, ASCII+1);
151 152
152 /* ... but make them available globally */ 153 /* ... but make them available globally */
153 poutput = output; 154 poutput = output;