[BusyBox] #define -> static const int

Larry Doolittle ldoolitt at recycle.lbl.gov
Fri Jan 5 23:57:39 UTC 2001


Mark -

I dunno about this.

-#define ESC    27
-#define DEL    127
+enum {
+       ESC = 27,
+       DEL = 127,
+};

Gack!  Put those back.

-static char               baseDestName[BUFSIZ + 1];
+static char               *baseDestName;

+       /* better than declaring a big static buffer */
+       baseDestName = xmalloc(sizeof(char)*(BUFSIZ + 1));

Depends.  

Comment 1: sizeof(char) is guaranteed to be exactly 1,
even in the presence of Cray's, SHARC's, and unicode.
Check the FAQs.

Erik and I just talked about the implications of
recursion via applets.  There are issues either way.
You could also
	static char               *baseDestName = NULL;
	baseDestName = xrealloc(baseDestName, BUFSIZ + 1);

It is presumably these cases that account for the
big bss savings, which Matt correctly points out is relevant
for the in-memory size, but not the on-disk size.
The bss size you saved should amount to:
       cp_mv.c  BUFSIZ+1
   rpmunpack.c  BUFSIZ
          tr.c  BUFSIZ*2+ASCII*3+3
Now, curiously, BUFSIZ is not defined by busybox.  On my
system, the libc header files define BUFSIZ to be 8192.
If that's true on your system, too, you have saved 33537 bytes
of bss by adding 7 calls to xmalloc().  Your printout claims
25888 bytes savings, I'm curious if you can explain the difference.

-#define is_cp 0
-#define is_mv 1
+static const int is_cp = 0;
+static const int is_mv = 1;

Although I haven't read the code for this piece, it
looks like a good candidate for enum.

-#define VT_DISALLOCATE  0x5608  /* free memory associated to vt */
+static const int VT_DISALLOCATE = 0x5608;  /* free memory associated to vt */

This is less clear than the ESC/DEL case above.  The
point of the preprocessor is to assign a human readable
token to what intrinsically is a "number with meaning".
That is different from the very reasonable cases you also
changed, like

-#define NR_KEYS         128
-#define MAX_NR_KEYMAPS  256
+static const int NR_KEYS = 128;
+static const int MAX_NR_KEYMAPS = 256;

Most of your changes are of one these last two styles.
Onward.

+enum {
+       CMD_HELP = 0,
+       CMD_FB = 1,
+       CMD_DB = 2,

Cool.  I had forgotten you could control the numerical value
of an enum.  I guess it's safe to use such an enum as an array
index?  I'll go back to parser.c and make the redirection type
an enum again.

     - Larry

P.S., the next time someone makes a massive change to the
source tree like this, could they also trim trailing blanks?





More information about the busybox mailing list