[BusyBox] start of ash optimizations
Aaron Lehmann
aaronl at vitelus.com
Sat Jul 7 02:47:10 UTC 2001
Can you tell that I'm tired?
-------------- next part --------------
Index: ash.c
===================================================================
RCS file: /var/cvs/busybox/ash.c,v
retrieving revision 1.7
diff -u -r1.7 ash.c
--- ash.c 2001/07/07 00:05:55 1.7
+++ ash.c 2001/07/07 08:39:32
@@ -1355,6 +1355,10 @@
static struct alias *freealias (struct alias *);
static struct alias **__lookupalias (const char *);
+static void freefunc(union node *n);
+
+
+
static void
setalias(name, val)
char *name, *val;
@@ -1809,8 +1813,88 @@
static char nullstr[1]; /* zero length string */
static char *curdir = nullstr; /* current working directory */
+
+static int funcblocksize; /* size of structures in function */
+static int funcstringsize; /* size of strings in node */
+static pointer funcblock; /* block to allocate function from */
+static char *funcstring; /* block to allocate strings from */
+
+static const short nodesize[26] = {
+ ALIGN(sizeof (struct nbinary)),
+ ALIGN(sizeof (struct ncmd)),
+ ALIGN(sizeof (struct npipe)),
+ ALIGN(sizeof (struct nredir)),
+ ALIGN(sizeof (struct nredir)),
+ ALIGN(sizeof (struct nredir)),
+ ALIGN(sizeof (struct nbinary)),
+ ALIGN(sizeof (struct nbinary)),
+ ALIGN(sizeof (struct nif)),
+ ALIGN(sizeof (struct nbinary)),
+ ALIGN(sizeof (struct nbinary)),
+ ALIGN(sizeof (struct nfor)),
+ ALIGN(sizeof (struct ncase)),
+ ALIGN(sizeof (struct nclist)),
+ ALIGN(sizeof (struct narg)),
+ ALIGN(sizeof (struct narg)),
+ ALIGN(sizeof (struct nfile)),
+ ALIGN(sizeof (struct nfile)),
+ ALIGN(sizeof (struct nfile)),
+ ALIGN(sizeof (struct nfile)),
+ ALIGN(sizeof (struct nfile)),
+ ALIGN(sizeof (struct ndup)),
+ ALIGN(sizeof (struct ndup)),
+ ALIGN(sizeof (struct nhere)),
+ ALIGN(sizeof (struct nhere)),
+ ALIGN(sizeof (struct nnot)),
+};
+
+
+static void calcsize (union node *);
+static void sizenodelist (struct nodelist *);
+static union node *copynode (union node *);
+static struct nodelist *copynodelist (struct nodelist *);
+static char *nodesavestr (char *);
static char *cdcomppath;
+/*
+ * When commands are first encountered, they are entered in a hash table.
+ * This ensures that a full path search will not have to be done for them
+ * on each invocation.
+ *
+ * We should investigate converting to a linear search, even though that
+ * would make the command name "hash" a misnomer.
+ */
+#define CMDTABLESIZE 31 /* should be prime */
+#define ARB 1 /* actual size determined at run time */
+
+
+
+struct tblentry {
+ struct tblentry *next; /* next entry in hash chain */
+ union param param; /* definition of builtin function */
+ short cmdtype; /* index identifying command */
+ char rehash; /* if set, cd done since entry created */
+ char cmdname[ARB]; /* name of command */
+};
+
+
+static struct tblentry *cmdtable[CMDTABLESIZE];
+static int builtinloc = -1; /* index in path of %builtin, or -1 */
+static int exerrno = 0; /* Last exec error */
+
+
+static void tryexec (char *, char **, char **);
+static void printentry (struct tblentry *, int);
+static void clearcmdentry (int);
+static struct tblentry *cmdlookup (const char *, int);
+static void delete_cmd_entry (void);
+#ifdef ASH_TYPE
+static int describe_command (char *, int);
+#endif
+static int path_change (const char *, int *);
+
+
+
static int
cdcmd(argc, argv)
int argc;
@@ -2424,7 +2508,6 @@
* exitstatus.
*/
static struct builtincmd *find_builtin (const char *);
-static void defun (char *, union node *);
static void
evaltree(n, flags)
@@ -2492,6 +2575,8 @@
break;
case NDEFUN: {
struct builtincmd *bcmd;
+ struct cmdentry entry;
+ struct tblentry *cmdp;
if (
(bcmd = find_builtin(n->narg.text)) &&
IS_BUILTIN_SPECIAL(bcmd)
@@ -2499,8 +2584,31 @@
out2fmt("%s is a special built-in\n", n->narg.text);
exitstatus = 1;
break;
+ }
+ entry.cmdtype = CMDFUNCTION;
+ if (!n->narg.next)
+ entry.u.func = NULL;
+ else
+ {
+ funcblocksize = 0;
+ funcstringsize = 0;
+ calcsize(n->narg.next);
+ funcblock = ckmalloc(funcblocksize + funcstringsize);
+ funcstring = (char *) funcblock + funcblocksize;
+ entry.u.func = copynode(n->narg.next);
+ }
+
+ /* Add a new command entry, replacing any existing command entry for
+ the same name. */
+ INTOFF;
+ cmdp = cmdlookup(n->narg.text, 1);
+ if (cmdp->cmdtype == CMDFUNCTION) {
+ freefunc(cmdp->param.func);
}
- defun(n->narg.text, n->narg.next);
+ cmdp->cmdtype = entry.cmdtype;
+ cmdp->param = entry.u;
+ INTON;
+
exitstatus = 0;
break;
}
@@ -3326,44 +3434,8 @@
out2fmt(" %s",sp->text);
}
}
-/*
- * When commands are first encountered, they are entered in a hash table.
- * This ensures that a full path search will not have to be done for them
- * on each invocation.
- *
- * We should investigate converting to a linear search, even though that
- * would make the command name "hash" a misnomer.
- */
-#define CMDTABLESIZE 31 /* should be prime */
-#define ARB 1 /* actual size determined at run time */
-
-
-struct tblentry {
- struct tblentry *next; /* next entry in hash chain */
- union param param; /* definition of builtin function */
- short cmdtype; /* index identifying command */
- char rehash; /* if set, cd done since entry created */
- char cmdname[ARB]; /* name of command */
-};
-
-static struct tblentry *cmdtable[CMDTABLESIZE];
-static int builtinloc = -1; /* index in path of %builtin, or -1 */
-static int exerrno = 0; /* Last exec error */
-
-
-static void tryexec (char *, char **, char **);
-static void printentry (struct tblentry *, int);
-static void clearcmdentry (int);
-static struct tblentry *cmdlookup (const char *, int);
-static void delete_cmd_entry (void);
-#ifdef ASH_TYPE
-static int describe_command (char *, int);
-#endif
-static int path_change (const char *, int *);
-
-
/*
* Exec a program. Never returns. If you change this routine, you may
* have to change the find_command routine as well.
@@ -4236,46 +4308,6 @@
INTON;
}
-
-
-/*
- * Add a new command entry, replacing any existing command entry for
- * the same name.
- */
-
-static void
-addcmdentry(char *name, struct cmdentry *entry)
-{
- struct tblentry *cmdp;
-
- INTOFF;
- cmdp = cmdlookup(name, 1);
- if (cmdp->cmdtype == CMDFUNCTION) {
- freefunc(cmdp->param.func);
- }
- cmdp->cmdtype = entry->cmdtype;
- cmdp->param = entry->u;
- INTON;
-}
-
-
-/*
- * Define a shell function.
- */
-
-static union node *copyfunc(union node *);
-
-static void
-defun(char *name, union node *func)
-{
- struct cmdentry entry;
-
- entry.cmdtype = CMDFUNCTION;
- entry.u.func = copyfunc(func);
- addcmdentry(name, &entry);
-}
-
-
/*
* Delete a function if it exists.
*/
@@ -6135,7 +6167,7 @@
}
-static int whichprompt; /* 1 == PS1, 2 == PS2 */
+static char whichprompt; /* 1 == PS1, 2 == PS2 */
struct redirtab {
@@ -8800,65 +8832,8 @@
*/
-static int funcblocksize; /* size of structures in function */
-static int funcstringsize; /* size of strings in node */
-static pointer funcblock; /* block to allocate function from */
-static char *funcstring; /* block to allocate strings from */
-static const short nodesize[26] = {
- ALIGN(sizeof (struct nbinary)),
- ALIGN(sizeof (struct ncmd)),
- ALIGN(sizeof (struct npipe)),
- ALIGN(sizeof (struct nredir)),
- ALIGN(sizeof (struct nredir)),
- ALIGN(sizeof (struct nredir)),
- ALIGN(sizeof (struct nbinary)),
- ALIGN(sizeof (struct nbinary)),
- ALIGN(sizeof (struct nif)),
- ALIGN(sizeof (struct nbinary)),
- ALIGN(sizeof (struct nbinary)),
- ALIGN(sizeof (struct nfor)),
- ALIGN(sizeof (struct ncase)),
- ALIGN(sizeof (struct nclist)),
- ALIGN(sizeof (struct narg)),
- ALIGN(sizeof (struct narg)),
- ALIGN(sizeof (struct nfile)),
- ALIGN(sizeof (struct nfile)),
- ALIGN(sizeof (struct nfile)),
- ALIGN(sizeof (struct nfile)),
- ALIGN(sizeof (struct nfile)),
- ALIGN(sizeof (struct ndup)),
- ALIGN(sizeof (struct ndup)),
- ALIGN(sizeof (struct nhere)),
- ALIGN(sizeof (struct nhere)),
- ALIGN(sizeof (struct nnot)),
-};
-
-static void calcsize (union node *);
-static void sizenodelist (struct nodelist *);
-static union node *copynode (union node *);
-static struct nodelist *copynodelist (struct nodelist *);
-static char *nodesavestr (char *);
-
-
-
-/*
- * Make a copy of a parse tree.
- */
-
-static union node *
-copyfunc(union node *n)
-{
- if (n == NULL)
- return NULL;
- funcblocksize = 0;
- funcstringsize = 0;
- calcsize(n);
- funcblock = ckmalloc(funcblocksize + funcstringsize);
- funcstring = (char *) funcblock + funcblocksize;
- return copynode(n);
-}
@@ -9680,7 +9655,7 @@
static int noexpand (char *);
static void synexpect (int) __attribute__((noreturn));
static void synerror (const char *) __attribute__((noreturn));
-static void setprompt (int);
+static void setprompt (char);
/*
@@ -9846,8 +9821,8 @@
n2->type = NNOT;
n2->nnot.com = n1;
return n2;
- } else
- return n1;
+ }
+ return n1;
}
@@ -11116,31 +11091,23 @@
/* NOTREACHED */
}
-
-/*
- * called by editline -- any expansions to the prompt
- * should be added here.
- */
-static inline const char *
-getprompt(void *unused)
-{
- switch (whichprompt) {
- case 0:
- return "";
- case 1:
- return ps1val();
- case 2:
- return ps2val();
- default:
- return "<internal prompt error>";
- }
-}
-
static void
-setprompt(int which)
+setprompt(char which)
{
+ char *s;
whichprompt = which;
- putprompt(getprompt(NULL));
+ switch (which)
+ {
+ case 1:
+ s = ps1val();
+ break;
+ case 2:
+ s = ps2val();
+ break;
+ default/* actually case 0, but this pacifies gcc */:
+ s = "";
+ }
+ putprompt(s);
}
@@ -11176,10 +11143,8 @@
int fd;
int newfd;
int try;
- char memory[10]; /* file descriptors to write to memory */
+ char memory[10] = {0,0,0,0,0,0,0,0,0,0}; /* file descriptors to write to memory */
- for (i = 10 ; --i >= 0 ; )
- memory[i] = 0;
memory[1] = flags & REDIR_BACKQ;
if (flags & REDIR_PUSH) {
sv = ckmalloc(sizeof (struct redirtab));
More information about the busybox
mailing list