From 4d3fc2413ebb9524d6ae113b24305f5ac1cdb868 Mon Sep 17 00:00:00 2001 From: tg Date: Wed, 12 Nov 2008 00:54:52 +0000 Subject: [PATCH] switch to a (nmemb,size) API from a (nmemb*size) API cost: text += (308, 256, 4, -100) --- alloc.c | 12 ++++++++---- edit.c | 26 +++++++++++++------------- eval.c | 10 +++++----- exec.c | 4 ++-- expr.c | 4 ++-- funcs.c | 15 +++++++-------- histrap.c | 14 +++++++------- jobs.c | 6 +++--- lex.c | 9 ++++----- main.c | 19 +++++++++---------- misc.c | 21 +++++++++++---------- sh.h | 34 +++++++++++++++++----------------- shf.c | 15 +++++++-------- syn.c | 25 +++++++++++-------------- tree.c | 38 +++++++++++++++++++------------------- var.c | 24 +++++++++++------------- 16 files changed, 136 insertions(+), 140 deletions(-) diff --git a/alloc.c b/alloc.c index bc07357..296d536 100644 --- a/alloc.c +++ b/alloc.c @@ -29,7 +29,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.10 2008/11/12 00:27:54 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.11 2008/11/12 00:54:46 tg Exp $"); struct link { struct link *prev; @@ -68,10 +68,12 @@ adelete(PArea *pap) #define P2L(p) ( (struct link *)(((ptrdiff_t)(p)) - sizeof (struct link)) ) void * -alloc(size_t size, PArea ap) +alloc(size_t nmemb, size_t size, PArea ap) { struct link *l; + size *= nmemb; + if ((l = malloc(sizeof (struct link) + size)) == NULL) internal_errorf("unable to allocate memory"); l->next = ap->freelist; @@ -84,12 +86,14 @@ alloc(size_t size, PArea ap) } void * -aresize(void *ptr, size_t size, PArea ap) +aresize(void *ptr, size_t nmemb, size_t size, PArea ap) { struct link *l, *l2, *lprev, *lnext; if (ptr == NULL) - return (alloc(size, ap)); + return (alloc(nmemb, size, ap)); + + size *= nmemb; l = P2L(ptr); lprev = l->prev; diff --git a/edit.c b/edit.c index c2f340f..dbce972 100644 --- a/edit.c +++ b/edit.c @@ -5,7 +5,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.143 2008/11/12 00:27:54 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.144 2008/11/12 00:54:46 tg Exp $"); /* tty driver characters we are interested in */ typedef struct { @@ -400,7 +400,7 @@ x_command_glob(int flags, const char *str, int slen, char ***wordsp) int i, path_order = 0; info = (struct path_order_info *) - alloc(sizeof (struct path_order_info) * nwords, ATEMP); + alloc(nwords, sizeof (struct path_order_info), ATEMP); for (i = 0; i < nwords; i++) { info[i].word = words[i]; info[i].base = x_basename(words[i], NULL); @@ -415,7 +415,7 @@ x_command_glob(int flags, const char *str, int slen, char ***wordsp) path_order_cmp); for (i = 0; i < nwords; i++) words[i] = info[i].word; - afree((void *)info, ATEMP); + afree(info, ATEMP); } else { /* Sort and remove duplicate entries */ char **words = (char **)XPptrv(w); @@ -2450,7 +2450,7 @@ x_push(int nchars) strndupx(cp, xcp, nchars, AEDIT); if (killstack[killsp]) - afree((void *)killstack[killsp], AEDIT); + afree(killstack[killsp], AEDIT); killstack[killsp] = cp; killsp = (killsp + 1) % KILLSIZE; } @@ -2661,7 +2661,7 @@ x_bind(const char *a1, const char *a2, if ((x_tab[prefix][key] & 0x7F) == XFUNC_ins_string && x_atab[prefix][key]) - afree((void *)x_atab[prefix][key], AEDIT); + afree(x_atab[prefix][key], AEDIT); x_tab[prefix][key] = f | (hastilde ? 0x80 : 0); x_atab[prefix][key] = sp; @@ -2684,7 +2684,7 @@ x_init_emacs(void) AEDIT = anew(); x_nextcmd = -1; - x_tab = (unsigned char (*)[X_TABSZ])alloc(sizeofN(*x_tab, X_NTABS), AEDIT); + x_tab = alloc(X_NTABS, sizeof (*x_tab), AEDIT); for (j = 0; j < X_TABSZ; j++) x_tab[0][j] = XFUNC_insert; for (i = 1; i < X_NTABS; i++) @@ -2694,7 +2694,7 @@ x_init_emacs(void) x_tab[x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char] = x_defbindings[i].xdb_func; - x_atab = (char *(*)[X_TABSZ])alloc(sizeofN(*x_atab, X_NTABS), AEDIT); + x_atab = alloc(X_NTABS, sizeof (*x_atab), AEDIT); for (i = 1; i < X_NTABS; i++) for (j = 0; j < X_TABSZ; j++) x_atab[i][j] = NULL; @@ -3600,8 +3600,8 @@ x_vi(char *buf, size_t len) if (!wbuf_len || wbuf_len != x_cols - 3) { wbuf_len = x_cols - 3; - wbuf[0] = aresize(wbuf[0], wbuf_len, APERM); - wbuf[1] = aresize(wbuf[1], wbuf_len, APERM); + wbuf[0] = aresize(wbuf[0], 1, wbuf_len, APERM); + wbuf[1] = aresize(wbuf[1], 1, wbuf_len, APERM); } (void)memset(wbuf[0], ' ', wbuf_len); (void)memset(wbuf[1], ' ', wbuf_len); @@ -4130,7 +4130,7 @@ vi_cmd(int argcnt, const char *cmd) nlen = strlen(ap->val.s) + 1; olen = !macro.p ? 2 : macro.len - (macro.p - macro.buf); - nbuf = alloc(nlen + 1 + olen, APERM); + nbuf = alloc(1, nlen + 1 + olen, APERM); memcpy(nbuf, ap->val.s, nlen); nbuf[nlen++] = cmd[1]; if (macro.p) { @@ -4777,8 +4777,8 @@ save_edstate(struct edstate *old) { struct edstate *new; - new = (struct edstate *)alloc(sizeof (struct edstate), APERM); - new->cbuf = alloc(old->cbufsize, APERM); + new = alloc(1, sizeof (struct edstate), APERM); + new->cbuf = alloc(1, old->cbufsize, APERM); memcpy(new->cbuf, old->cbuf, old->linelen); new->cbufsize = old->cbufsize; new->linelen = old->linelen; @@ -4801,7 +4801,7 @@ static void free_edstate(struct edstate *old) { afree(old->cbuf, APERM); - afree((char *)old, APERM); + afree(old, APERM); } /* diff --git a/eval.c b/eval.c index f867467..d0fa5b8 100644 --- a/eval.c +++ b/eval.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.49 2008/10/28 14:32:39 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.50 2008/11/12 00:54:47 tg Exp $"); #ifdef MKSH_SMALL #define MKSH_NOPWNAM @@ -297,7 +297,7 @@ expand(const char *cp, /* input word */ if (!st->next) { SubType *newst; - newst = (SubType *)alloc( + newst = alloc(1, sizeof (SubType), ATEMP); newst->next = NULL; newst->prev = st; @@ -590,7 +590,7 @@ expand(const char *cp, /* input word */ */ len = strlen(dp) + 1; setstr(st->var, - debunk((char *)alloc(len, ATEMP), + debunk(alloc(1, len, ATEMP), dp, len), KSH_UNWIND_ERROR); x.str = str_val(st->var); type = XSUB; @@ -770,7 +770,7 @@ expand(const char *cp, /* input word */ Xlength(ds, dp) == 0) { char *p; - *(p = alloc(1, ATEMP)) = '\0'; + *(p = alloc(1, 1, ATEMP)) = '\0'; XPput(*wp, p); } type = XSUBMID; @@ -1474,7 +1474,7 @@ alt_expand(XPtrV *wp, char *start, char *exp_start, char *end, int fdo) l1 = brace_start - start; l2 = (p - 1) - field_start; l3 = end - brace_end; - new = (char *)alloc(l1 + l2 + l3 + 1, ATEMP); + new = alloc(1, l1 + l2 + l3 + 1, ATEMP); memcpy(new, start, l1); memcpy(new + l1, field_start, l2); memcpy(new + l1 + l2, brace_end, l3); diff --git a/exec.c b/exec.c index 23cc6e5..078ef3e 100644 --- a/exec.c +++ b/exec.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.45 2008/10/28 14:32:39 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.46 2008/11/12 00:54:47 tg Exp $"); static int comexec(struct op *, struct tbl *volatile, const char **, int volatile); @@ -72,7 +72,7 @@ execute(struct op *volatile t, flags &= ~XTIME; if (t->ioact != NULL || t->type == TPIPE || t->type == TCOPROC) { - e->savefd = (short *)alloc(sizeofN(short, NUFILE), ATEMP); + e->savefd = alloc(NUFILE, sizeof (short), ATEMP); /* initialise to not redirected */ memset(e->savefd, 0, sizeofN(short, NUFILE)); } diff --git a/expr.c b/expr.c index bbd4c1f..1c52903 100644 --- a/expr.c +++ b/expr.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.19 2008/10/28 14:32:40 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.20 2008/11/12 00:54:48 tg Exp $"); /* The order of these enums is constrained by the order of opinfo[] */ enum token { @@ -544,7 +544,7 @@ tempvar(void) { struct tbl *vp; - vp = (struct tbl *)alloc(sizeof (struct tbl), ATEMP); + vp = alloc(1, sizeof (struct tbl), ATEMP); vp->flag = ISSET|INTEGER; vp->type = 0; vp->areap = ATEMP; diff --git a/funcs.c b/funcs.c index 63c2b4f..e3ac52e 100644 --- a/funcs.c +++ b/funcs.c @@ -5,7 +5,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.91 2008/11/12 00:27:55 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.92 2008/11/12 00:54:48 tg Exp $"); /* A leading = means assignments before command are kept; * a leading * means a POSIX special builtin; @@ -202,7 +202,7 @@ c_cd(const char **wp) olen = strlen(wp[0]); nlen = strlen(wp[1]); elen = strlen(current_wd + ilen + olen) + 1; - dir = allocd = alloc(ilen + nlen + elen, ATEMP); + dir = allocd = alloc(1, ilen + nlen + elen, ATEMP); memcpy(dir, current_wd, ilen); memcpy(dir + ilen, wp[1], nlen); memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen); @@ -1102,7 +1102,7 @@ c_alias(const char **wp) if ((val && !tflag) || (!val && tflag && !Uflag)) { if (ap->flag&ALLOC) { ap->flag &= ~(ALLOC|ISSET); - afree((void*)ap->val.s, APERM); + afree(ap->val.s, APERM); } /* ignore values for -t (at&t ksh does this) */ newval = tflag ? search(alias, path, X_OK, NULL) : val; @@ -1159,7 +1159,7 @@ c_unalias(const char **wp) } if (ap->flag&ALLOC) { ap->flag &= ~(ALLOC|ISSET); - afree((void*)ap->val.s, APERM); + afree(ap->val.s, APERM); } ap->flag &= ~(DEFINED|ISSET|EXPORT); } @@ -1170,7 +1170,7 @@ c_unalias(const char **wp) for (ktwalk(&ts, t); (ap = ktnext(&ts)); ) { if (ap->flag&ALLOC) { ap->flag &= ~(ALLOC|ISSET); - afree((void*)ap->val.s, APERM); + afree(ap->val.s, APERM); } ap->flag &= ~(DEFINED|ISSET|EXPORT); } @@ -2113,8 +2113,7 @@ c_set(const char **wp) while (*++wp != NULL) strdupx(*wp, *wp, l->areap); l->argc = wp - owp - 1; - l->argv = (const char **)alloc(sizeofN(char *, l->argc+2), - l->areap); + l->argv = alloc(l->argc + 2, sizeof (char *), l->areap); for (wp = l->argv; (*wp++ = *owp++) != NULL; ) ; } @@ -3065,7 +3064,7 @@ c_realpath(const char **wp) else { char *buf; - if (realpath(*wp, (buf = alloc(PATH_MAX, ATEMP))) == NULL) { + if (realpath(*wp, (buf = alloc(1, PATH_MAX, ATEMP))) == NULL) { rv = errno; bi_errorf("%s: %s", *wp, strerror(rv)); } else diff --git a/histrap.c b/histrap.c index 336eb31..2d05fdd 100644 --- a/histrap.c +++ b/histrap.c @@ -3,7 +3,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.74 2008/11/09 20:32:17 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.75 2008/11/12 00:54:48 tg Exp $"); /*- * MirOS: This is the default mapping type, and need not be specified. @@ -68,7 +68,7 @@ c_fc(const char **wp) sflag++; else { size_t len = strlen(p); - editor = alloc(len + 4, ATEMP); + editor = alloc(1, len + 4, ATEMP); memcpy(editor, p, len); memcpy(editor + len, " $_", 4); } @@ -414,7 +414,7 @@ histbackup(void) if (histptr >= history && last_line != hist_source->line) { hist_source->line--; - afree((void*)*histptr, APERM); + afree(*histptr, APERM); histptr--; last_line = hist_source->line; } @@ -504,7 +504,7 @@ sethistsize(int n) cursize = n; } - history = (char **)aresize(history, n*sizeof(char *), APERM); + history = aresize(history, n, sizeof (char *), APERM); histsize = n; histptr = history + cursize; @@ -555,7 +555,7 @@ init_histvec(void) { if (history == (char **)NULL) { histsize = HISTORYSIZE; - history = (char **)alloc(histsize * sizeof (char *), APERM); + history = alloc(histsize, sizeof (char *), APERM); histptr = history - 1; } } @@ -597,7 +597,7 @@ histsave(int *lnp, const char *cmd, bool dowrite __unused, bool ignoredups) hp = histptr; if (++hp >= history + histsize) { /* remove oldest command */ - afree((void*)*history, APERM); + afree(*history, APERM); for (hp = history; hp < history + histsize - 1; hp++) hp[0] = hp[1]; } @@ -884,7 +884,7 @@ histinsert(Source *s, int lno, const char *line) if (lno >= s->line - (histptr - history) && lno <= s->line) { hp = &histptr[lno - s->line]; if (*hp) - afree((void*)*hp, APERM); + afree(*hp, APERM); strdupx(*hp, line, APERM); } } diff --git a/jobs.c b/jobs.c index 3d2f08c..0869c16 100644 --- a/jobs.c +++ b/jobs.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.39 2008/11/09 20:32:18 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.40 2008/11/12 00:54:49 tg Exp $"); /* Order important! */ #define PRUNNING 0 @@ -1397,7 +1397,7 @@ new_job(void) newj = free_jobs; free_jobs = free_jobs->next; } else - newj = (Job *)alloc(sizeof (Job), APERM); + newj = alloc(1, sizeof (Job), APERM); /* brute force method */ for (i = 1; ; i++) { @@ -1424,7 +1424,7 @@ new_proc(void) p = free_procs; free_procs = free_procs->next; } else - p = (Proc *)alloc(sizeof (Proc), APERM); + p = alloc(1, sizeof (Proc), APERM); return p; } diff --git a/lex.c b/lex.c index 7a9e205..17ea96b 100644 --- a/lex.c +++ b/lex.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.75 2008/11/11 23:50:29 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.76 2008/11/12 00:54:49 tg Exp $"); /* * states while lexing word @@ -776,8 +776,7 @@ yylex(int cf) dp = Xstring(ws, wp); if ((c == '<' || c == '>' || c == '&') && state == SBASE) { - struct ioword *iop = (struct ioword *)alloc(sizeof (*iop), - ATEMP); + struct ioword *iop = alloc(1, sizeof (struct ioword), ATEMP); if (Xlength(ws, wp) == 0) iop->unit = c == '<' ? 0 : 1; @@ -1035,7 +1034,7 @@ pushs(int type, PArea areap) { Source *s; - s = (Source *)alloc(sizeof (Source), areap); + s = alloc(1, sizeof (Source), areap); s->type = type; s->str = null; s->start = NULL; @@ -1518,7 +1517,7 @@ getsc_bn(void) static Lex_state * push_state_(State_info *si, Lex_state *old_end) { - Lex_state *new = alloc(sizeof (Lex_state) * STATE_BSIZE, ATEMP); + Lex_state *new = alloc(STATE_BSIZE, sizeof (Lex_state), ATEMP); new[0].ls_info.base = old_end; si->base = &new[0]; diff --git a/main.c b/main.c index 3253bec..b93f1d6 100644 --- a/main.c +++ b/main.c @@ -13,7 +13,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/main.c,v 1.108 2008/11/12 00:27:56 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/main.c,v 1.109 2008/11/12 00:54:49 tg Exp $"); extern char **environ; @@ -131,7 +131,7 @@ main(int argc, const char *argv[]) #else #ifdef _CS_PATH if ((k = confstr(_CS_PATH, NULL, 0)) != (size_t)-1 && k > 0 && - confstr(_CS_PATH, cp = alloc(k + 1, APERM), k + 1) == k + 1) + confstr(_CS_PATH, cp = alloc(1, k + 1, APERM), k + 1) == k + 1) def_path = cp; else #endif @@ -607,7 +607,7 @@ newenv(int type) { struct env *ep; - ep = (struct env *)alloc(sizeof (*ep), ATEMP); + ep = alloc(1, sizeof (struct env), ATEMP); ep->type = type; ep->flags = 0; ep->areap = anew(); @@ -1153,7 +1153,7 @@ maketemp(PArea ap, Temp_type type, struct temp **tlist) pathname = tempnam(dir, "mksh."); len = ((pathname == NULL) ? 0 : strlen(pathname)) + 1; #endif - tp = (struct temp *)alloc(sizeof (struct temp) + len, ap); + tp = alloc(1, sizeof (struct temp) + len, ap); tp->name = (char *)&tp[1]; #if !HAVE_MKSTEMP if (pathname == NULL) @@ -1213,7 +1213,7 @@ texpand(struct table *tp, int nsize) struct tbl **ntblp, **otblp = tp->tbls; int osize = tp->size; - ntblp = (struct tbl **)alloc(sizeofN(struct tbl *, nsize), tp->areap); + ntblp = alloc(nsize, sizeof (struct tbl *), tp->areap); for (i = 0; i < nsize; i++) ntblp[i] = NULL; tp->size = nsize; @@ -1231,10 +1231,10 @@ texpand(struct table *tp, int nsize) *p = tblp; tp->nfree--; } else if (!(tblp->flag & FINUSE)) { - afree((void *)tblp, tp->areap); + afree(tblp, tp->areap); } } - afree((void *)otblp, tp->areap); + afree(otblp, tp->areap); } /* table */ @@ -1286,8 +1286,7 @@ ktenter(struct table *tp, const char *n, unsigned int h) } /* create new tbl entry */ len = strlen(n) + 1; - p = (struct tbl *)alloc(offsetof(struct tbl, name[0])+len, - tp->areap); + p = alloc(1, offsetof(struct tbl, name[0]) + len, tp->areap); p->flag = 0; p->type = 0; p->areap = tp->areap; @@ -1334,7 +1333,7 @@ ktsort(struct table *tp) size_t i; struct tbl **p, **sp, **dp; - p = (struct tbl **)alloc(sizeofN(struct tbl *, tp->size + 1), ATEMP); + p = alloc(tp->size + 1, sizeof (struct tbl *), ATEMP); sp = tp->tbls; /* source */ dp = p; /* dest */ for (i = 0; i < (size_t)tp->size; i++) diff --git a/misc.c b/misc.c index f506ec0..ffbd682 100644 --- a/misc.c +++ b/misc.c @@ -6,7 +6,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.90 2008/11/11 23:50:30 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.91 2008/11/12 00:54:50 tg Exp $"); #undef USE_CHVT #if defined(TIOCSCTTY) && !defined(MKSH_SMALL) @@ -70,7 +70,7 @@ Xcheck_grow_(XString *xsp, const char *xp, unsigned int more) const char *old_beg = xsp->beg; xsp->len += more > xsp->len ? more : xsp->len; - xsp->beg = aresize(xsp->beg, xsp->len + 8, xsp->areap); + xsp->beg = aresize(xsp->beg, 1, xsp->len + 8, xsp->areap); xsp->end = xsp->beg + xsp->len; return xsp->beg + (xp - old_beg); } @@ -493,8 +493,7 @@ gmatchx(const char *s, const char *p, bool isfile) if (!isfile && !has_globbing(p, pe)) { size_t len = pe - p + 1; char tbuf[64]; - char *t = len <= sizeof(tbuf) ? tbuf : - (char *)alloc(len, ATEMP); + char *t = len <= sizeof(tbuf) ? tbuf : alloc(1, len, ATEMP); debunk(t, p, len); return !strcmp(t, s); } @@ -934,7 +933,7 @@ print_columns(struct shf *shf, int n, char *(*func) (const void *, int, char *, int), const void *arg, int max_width, int prefcol) { - char *str = (char *)alloc(max_width + 1, ATEMP); + char *str = alloc(1, max_width + 1, ATEMP); int i, r, c, rows, cols, nspace; /* max_width + 1 for the space. Note that no space @@ -1049,8 +1048,8 @@ ksh_get_wd(size_t *dlen) char *ret, *b; size_t len = 1; - if ((ret = getcwd((b = alloc(PATH_MAX + 1, ATEMP)), PATH_MAX))) - ret = aresize(b, len = (strlen(b) + 1), ATEMP); + if ((ret = getcwd((b = alloc(1, PATH_MAX + 1, ATEMP)), PATH_MAX))) + ret = aresize(b, 1, len = (strlen(b) + 1), ATEMP); else afree(b, ATEMP); @@ -1228,8 +1227,10 @@ set_current_wd(char *pathl) } else len = strlen(p) + 1; - if (len > current_wd_size) - current_wd = aresize(current_wd, current_wd_size = len, APERM); + if (len > current_wd_size) { + afree(current_wd, APERM); + current_wd = alloc(1, current_wd_size = len, APERM); + } memcpy(current_wd, p, len); if (p != pathl && p != null) afree(p, ATEMP); @@ -1430,7 +1431,7 @@ strndup_(const char *src, size_t len, PArea ap) char *dst = NULL; if (src != NULL) { - dst = alloc(++len, ap); + dst = alloc(1, ++len, ap); strlcpy(dst, src, len); } return (dst); diff --git a/sh.h b/sh.h index 92ff555..72ca674 100644 --- a/sh.h +++ b/sh.h @@ -103,7 +103,7 @@ #define __SCCSID(x) __IDSTRING(sccsid,x) #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.254 2008/11/12 00:27:56 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.255 2008/11/12 00:54:50 tg Exp $"); #endif #define MKSH_VERSION "R36 2008/11/10" @@ -363,7 +363,7 @@ char *ucstrstr(char *, const char *); \ if (strdup_src != NULL) { \ size_t strdup_len = strlen(strdup_src) + 1; \ - strdup_dst = alloc(strdup_len, (ap)); \ + strdup_dst = alloc(1, strdup_len, (ap)); \ strlcpy(strdup_dst, strdup_src, strdup_len); \ } \ (d) = strdup_dst; \ @@ -374,7 +374,7 @@ char *ucstrstr(char *, const char *); \ if (strdup_src != NULL) { \ size_t strdup_len = (n) + 1; \ - strdup_dst = alloc(strdup_len, (ap)); \ + strdup_dst = alloc(1, strdup_len, (ap)); \ strlcpy(strdup_dst, strdup_src, strdup_len); \ } \ (d) = strdup_dst; \ @@ -1065,7 +1065,7 @@ typedef char *XStringP; #define XinitN(xs, length, area) do { \ (xs).len = (length); \ (xs).areap = (area); \ - (xs).beg = alloc((xs).len + X_EXTRA, (xs).areap); \ + (xs).beg = alloc(1, (xs).len + X_EXTRA, (xs).areap); \ (xs).end = (xs).beg + (xs).len; \ } while (0) #define Xinit(xs, xp, length, area) do { \ @@ -1087,10 +1087,10 @@ typedef char *XStringP; #define Xcheck(xs, xp) XcheckN((xs), (xp), 1) /* free string */ -#define Xfree(xs, xp) afree((void*)(xs).beg, (xs).areap) +#define Xfree(xs, xp) afree((xs).beg, (xs).areap) /* close, return string */ -#define Xclose(xs, xp) (char*)aresize((void*)(xs).beg, \ +#define Xclose(xs, xp) aresize((void*)(xs).beg, 1, \ (size_t)((xp) - (xs).beg), (xs).areap) /* begin of string */ #define Xstring(xs, xp) ((xs).beg) @@ -1114,16 +1114,16 @@ typedef struct XPtrV { #define XPinit(x, n) do { \ void **vp__; \ - vp__ = (void**)alloc(sizeofN(void*, (n)), ATEMP); \ + vp__ = alloc((n), sizeof (void *), ATEMP); \ (x).cur = (x).beg = vp__; \ (x).end = vp__ + (n); \ } while (0) #define XPput(x, p) do { \ if ((x).cur >= (x).end) { \ - int n = XPsize(x); \ - (x).beg = (void**) aresize((void*) (x).beg, \ - sizeofN(void *, n * 2), ATEMP); \ + size_t n = XPsize(x); \ + (x).beg = aresize((void*)(x).beg, \ + n * 2, sizeof (void *), ATEMP); \ (x).cur = (x).beg + n; \ (x).end = (x).cur + n; \ } \ @@ -1133,10 +1133,10 @@ typedef struct XPtrV { #define XPptrv(x) ((x).beg) #define XPsize(x) ((x).cur - (x).beg) -#define XPclose(x) (void**)aresize((void*)(x).beg, \ - sizeofN(void *, XPsize(x)), ATEMP) +#define XPclose(x) aresize((void*)(x).beg, \ + XPsize(x), sizeof (void *), ATEMP) -#define XPfree(x) afree((void *)(x).beg, ATEMP) +#define XPfree(x) afree((x).beg, ATEMP) #define IDENT 64 @@ -1248,11 +1248,11 @@ EXTERN int histsize; /* history size */ EXTERN struct timeval j_usrtime, j_systime; /* alloc.c */ -PArea anew(void); /* cannot fail */ +PArea anew(void); /* cannot fail */ void adelete(PArea *); -void *alloc(size_t, PArea); /* cannot fail */ -void *aresize(void *, size_t, PArea); -void afree(void *, PArea); /* can take NULL */ +void *alloc(size_t, size_t, PArea); /* cannot fail */ +void *aresize(void *, size_t, size_t, PArea); +void afree(void *, PArea); /* can take NULL */ /* edit.c */ void x_init(void); int x_read(char *, size_t); diff --git a/shf.c b/shf.c index 3dce4bd..264f47e 100644 --- a/shf.c +++ b/shf.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.22 2008/10/10 21:30:43 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.23 2008/11/12 00:54:51 tg Exp $"); /* flags to shf_emptybuf() */ #define EB_READSW 0x01 /* about to switch to reading */ @@ -29,7 +29,7 @@ shf_open(const char *name, int oflags, int mode, int sflags) int fd; /* Done before open so if alloca fails, fd won't be lost. */ - shf = (struct shf *)alloc(sizeof (struct shf) + bsize, ATEMP); + shf = alloc(1, sizeof (struct shf) + bsize, ATEMP); shf->areap = ATEMP; shf->buf = (unsigned char *)&shf[1]; shf->bsize = bsize; @@ -92,12 +92,12 @@ shf_fdopen(int fd, int sflags, struct shf *shf) if (shf) { if (bsize) { - shf->buf = (unsigned char *)alloc(bsize, ATEMP); + shf->buf = alloc(1, bsize, ATEMP); sflags |= SHF_ALLOCB; } else shf->buf = NULL; } else { - shf = (struct shf *)alloc(sizeof (struct shf) + bsize, ATEMP); + shf = alloc(1, sizeof (struct shf) + bsize, ATEMP); shf->buf = (unsigned char *)&shf[1]; sflags |= SHF_ALLOCS; } @@ -180,7 +180,7 @@ shf_sopen(char *buf, int bsize, int sflags, struct shf *shf) internal_errorf("shf_sopen: flags 0x%x", sflags); if (!shf) { - shf = (struct shf *)alloc(sizeof (struct shf), ATEMP); + shf = alloc(1, sizeof (struct shf), ATEMP); sflags |= SHF_ALLOCS; } shf->areap = ATEMP; @@ -188,7 +188,7 @@ shf_sopen(char *buf, int bsize, int sflags, struct shf *shf) if (bsize <= 0) bsize = 64; sflags |= SHF_ALLOCB; - buf = alloc(bsize, shf->areap); + buf = alloc(1, bsize, shf->areap); } shf->fd = -1; shf->buf = shf->rp = shf->wp = (unsigned char *)buf; @@ -323,8 +323,7 @@ shf_emptybuf(struct shf *shf, int flags) !(shf->flags & SHF_ALLOCB)) return EOF; /* allocate more space for buffer */ - nbuf = (unsigned char *)aresize(shf->buf, shf->wbsize * 2, - shf->areap); + nbuf = aresize(shf->buf, 2, shf->wbsize, shf->areap); shf->rp = nbuf + (shf->rp - shf->buf); shf->wp = nbuf + (shf->wp - shf->buf); shf->rbsize += shf->wbsize; diff --git a/syn.c b/syn.c index 62635db..47ab472 100644 --- a/syn.c +++ b/syn.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.29 2008/11/09 19:48:02 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.30 2008/11/12 00:54:51 tg Exp $"); struct nesting_state { int start_token; /* token than began nesting (eg, FOR) */ @@ -170,7 +170,7 @@ synio(int cf) iop->name = yylval.cp; if (iop->flag & IOBASH) { - nextiop = (struct ioword *)alloc(sizeof (*iop), ATEMP); + nextiop = alloc(1, sizeof (*iop), ATEMP); iop->flag &= ~IOBASH; nextiop->unit = 2; @@ -204,8 +204,7 @@ get_command(int cf) XPtrV args, vars; struct nesting_state old_nesting; - iops = (struct ioword **)alloc(sizeofN(struct ioword *, NUFILE+1), - ATEMP); + iops = alloc(NUFILE + 1, sizeof (struct ioword *), ATEMP); XPinit(args, 16); XPinit(vars, 16); @@ -213,7 +212,7 @@ get_command(int cf) switch (c = token(cf|KEYWORD|ALIAS|VARASN)) { default: REJECT; - afree((void*) iops, ATEMP); + afree(iops, ATEMP); XPfree(args); XPfree(vars); return NULL; /* empty line */ @@ -412,7 +411,7 @@ get_command(int cf) syniocf &= ~(KEYWORD|ALIAS); t = pipeline(0); if (t) { - t->str = alloc(2, ATEMP); + t->str = alloc(1, 2, ATEMP); t->str[0] = '\0'; /* TF_* flags */ t->str[1] = '\0'; } @@ -432,12 +431,11 @@ get_command(int cf) } if (iopn == 0) { - afree((void*)iops, ATEMP); + afree(iops, ATEMP); t->ioact = NULL; } else { iops[iopn++] = NULL; - iops = (struct ioword **) aresize((void*) iops, - sizeofN(struct ioword *, iopn), ATEMP); + iops = aresize(iops, iopn, sizeof (struct ioword *), ATEMP); t->ioact = iops; } @@ -612,14 +610,13 @@ function_body(char *name, * be used as input), we pretend there is a colon here. */ t->left = newtp(TCOM); - t->left->args = (const char **)alloc(sizeof (char *) * 2, - ATEMP); - t->left->args[0] = tv = alloc(sizeof (char) * 3, ATEMP); + t->left->args = alloc(2, sizeof (char *), ATEMP); + t->left->args[0] = tv = alloc(3, sizeof (char), ATEMP); tv[0] = CHAR; tv[1] = ':'; tv[2] = EOS; t->left->args[1] = NULL; - t->left->vars = (char **)alloc(sizeof (char *), ATEMP); + t->left->vars = alloc(1, sizeof (char *), ATEMP); t->left->vars[0] = NULL; t->left->lineno = 1; } @@ -796,7 +793,7 @@ newtp(int type) { struct op *t; - t = (struct op *)alloc(sizeof (*t), ATEMP); + t = alloc(1, sizeof (struct op), ATEMP); t->type = type; t->u.evalflags = 0; t->args = NULL; diff --git a/tree.c b/tree.c index a3fbb0c..69b1c26 100644 --- a/tree.c +++ b/tree.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.22 2008/11/11 23:50:31 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.23 2008/11/12 00:54:51 tg Exp $"); #define INDENT 4 @@ -421,7 +421,7 @@ tcopy(struct op *t, PArea ap) if (t == NULL) return NULL; - r = (struct op *)alloc(sizeof (struct op), ap); + r = alloc(1, sizeof (struct op), ap); r->type = t->type; r->u.evalflags = t->u.evalflags; @@ -436,8 +436,8 @@ tcopy(struct op *t, PArea ap) else { for (tw = (const char **)t->vars; *tw++ != NULL; ) ; - rw = r->vars = (char **)alloc((tw - - (const char **)t->vars + 1) * sizeof (*tw), ap); + rw = r->vars = alloc((tw - (const char **)t->vars + 1), + sizeof (*tw), ap); for (tw = (const char **)t->vars; *tw != NULL; ) *rw++ = wdcopy(*tw++, ap); *rw = NULL; @@ -448,8 +448,8 @@ tcopy(struct op *t, PArea ap) else { for (tw = t->args; *tw++ != NULL; ) ; - r->args = (const char **)(rw = (char **)alloc((tw - - t->args + 1) * sizeof (*tw), ap)); + r->args = (const char **)(rw = alloc((tw - t->args + 1), + sizeof (*tw), ap)); for (tw = t->args; *tw != NULL; ) *rw++ = wdcopy(*tw++, ap); *rw = NULL; @@ -468,7 +468,7 @@ char * wdcopy(const char *wp, PArea ap) { size_t len = wdscan(wp, EOS) - wp; - return memcpy(alloc(len, ap), wp, len); + return memcpy(alloc(1, len, ap), wp, len); } /* return the position of prefix c in wp plus 1 */ @@ -618,13 +618,13 @@ iocopy(struct ioword **iow, PArea ap) for (ior = iow; *ior++ != NULL; ) ; - ior = (struct ioword **)alloc((ior - iow + 1) * sizeof (*ior), ap); + ior = alloc((ior - iow + 1), sizeof (struct ioword *), ap); for (i = 0; iow[i] != NULL; i++) { struct ioword *p, *q; p = iow[i]; - q = (struct ioword *)alloc(sizeof (*p), ap); + q = alloc(1, sizeof (struct ioword), ap); ior[i] = q; *q = *p; if (p->name != NULL) @@ -651,12 +651,12 @@ tfree(struct op *t, PArea ap) return; if (t->str != NULL) - afree((void*)t->str, ap); + afree(t->str, ap); if (t->vars != NULL) { for (w = t->vars; *w != NULL; w++) - afree((void*)*w, ap); - afree((void*)t->vars, ap); + afree(*w, ap); + afree(t->vars, ap); } if (t->args != NULL) { @@ -664,8 +664,8 @@ tfree(struct op *t, PArea ap) /* XXX we assume the caller is right */ cw.ro = t->args; for (w = cw.rw; *w != NULL; w++) - afree((void*)*w, ap); - afree((void*)t->args, ap); + afree(*w, ap); + afree(t->args, ap); } if (t->ioact != NULL) @@ -674,7 +674,7 @@ tfree(struct op *t, PArea ap) tfree(t->left, ap); tfree(t->right, ap); - afree((void*)t, ap); + afree(t, ap); } static void @@ -685,12 +685,12 @@ iofree(struct ioword **iow, PArea ap) for (iop = iow; (p = *iop++) != NULL; ) { if (p->name != NULL) - afree((void*)p->name, ap); + afree(p->name, ap); if (p->delim != NULL) - afree((void*)p->delim, ap); + afree(p->delim, ap); if (p->heredoc != NULL) - afree((void*)p->heredoc, ap); - afree((void*)p, ap); + afree(p->heredoc, ap); + afree(p, ap); } afree(iow, ap); } diff --git a/var.c b/var.c index ed1d8a9..4392b52 100644 --- a/var.c +++ b/var.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/var.c,v 1.59 2008/11/12 00:27:57 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/var.c,v 1.60 2008/11/12 00:54:52 tg Exp $"); /* * Variables @@ -37,7 +37,7 @@ newblock(void) struct block *l; static const char *empty[] = { null }; - l = (struct block *)alloc(sizeof (struct block), ATEMP); + l = alloc(1, sizeof (struct block), ATEMP); l->flags = 0; l->areap = anew(); /* TODO: could use e->area */ if (!e->loc) { @@ -367,7 +367,7 @@ setstr(struct tbl *vq, const char *s, int error_ok) internal_errorf( "setstr: %s=%s: assigning to self", vq->name, s); - afree((void*)vq->val.s, vq->areap); + afree(vq->val.s, vq->areap); } vq->flag &= ~(ISSET|ALLOC); vq->type = 0; @@ -523,7 +523,7 @@ formatstr(struct tbl *vp, const char *s) } else nlen = olen; - p = (char *)alloc((psiz = nlen * /* MB_LEN_MAX */ 3 + 1), ATEMP); + p = alloc(1, (psiz = nlen * /* MB_LEN_MAX */ 3 + 1), ATEMP); if (vp->flag & (RJUST|LJUST)) { int slen = olen, i = 0; @@ -595,14 +595,14 @@ export(struct tbl *vp, const char *val) int vallen = strlen(val) + 1; vp->flag |= ALLOC; - xp = (char*)alloc(namelen + 1 + vallen, vp->areap); + xp = alloc(1, namelen + 1 + vallen, vp->areap); memcpy(vp->val.s = xp, vp->name, namelen); xp += namelen; *xp++ = '='; vp->type = xp - vp->val.s; /* offset to value */ memcpy(xp, val, vallen); if (op != NULL) - afree((void*)op, vp->areap); + afree(op, vp->areap); } /* @@ -721,14 +721,13 @@ typeset(const char *var, Tflag set, Tflag clr, int field, int base) t->flag &= ~ISSET; else { if (t->flag & ALLOC) - afree((void*) t->val.s, - t->areap); + afree(t->val.s, t->areap); t->flag &= ~(ISSET|ALLOC); t->type = 0; } } if (free_me) - afree((void *) free_me, t->areap); + afree(free_me, t->areap); } } if (!ok) @@ -762,7 +761,7 @@ void unset(struct tbl *vp, int array_ref) { if (vp->flag & ALLOC) - afree((void*)vp->val.s, vp->areap); + afree(vp->val.s, vp->areap); if ((vp->flag & ARRAY) && !array_ref) { struct tbl *a, *tmp; @@ -771,7 +770,7 @@ unset(struct tbl *vp, int array_ref) tmp = a; a = a->u.array; if (tmp->flag & ALLOC) - afree((void *) tmp->val.s, tmp->areap); + afree(tmp->val.s, tmp->areap); afree(tmp, tmp->areap); } vp->u.array = NULL; @@ -1199,8 +1198,7 @@ arraysearch(struct tbl *vp, uint32_t val) else new = curr; } else - new = (struct tbl *)alloc(sizeof (struct tbl) + namelen, - vp->areap); + new = alloc(1, sizeof (struct tbl) + namelen, vp->areap); strlcpy(new->name, vp->name, namelen); new->flag = vp->flag & ~(ALLOC|DEFINED|ISSET|SPECIAL); new->type = vp->type;