* back out almost all of the memory allocator related changes, as aalloc

was hard to type and hard to fix, galloc is also hard to fix, and some
  things I learned will probably improve things more but make me use the
  original form as base (especially for space savings)
* let sizeofN die though, remove even more casts
* optimise, polish
* regen Makefiles
* sprinkle a few /* CONSTCOND */ while here
This commit is contained in:
tg 2008-12-13 17:02:18 +00:00
parent 732e10c982
commit 31d1499219
19 changed files with 194 additions and 215 deletions

View File

@ -1,23 +1,16 @@
# $MirOS: src/bin/mksh/Makefile,v 1.70 2008/11/17 01:14:57 tg Exp $ # $MirOS: src/bin/mksh/Makefile,v 1.71 2008/12/13 17:02:10 tg Exp $
#- #-
# use CPPFLAGS=-DDEBUG __CRAZY=Yes to check for certain more stuff # use CPPFLAGS=-DDEBUG __CRAZY=Yes to check for certain more stuff
.include <bsd.own.mk> .include <bsd.own.mk>
PROG= mksh PROG= mksh
.ifdef TEST SRCS= alloc.c edit.c eval.c exec.c expr.c funcs.c histrap.c \
SRCS= aalloc.c
.else
SRCS= alloc.c
.endif
SRCS+= edit.c eval.c exec.c expr.c funcs.c histrap.c \
jobs.c lex.c main.c misc.c shf.c syn.c tree.c var.c jobs.c lex.c main.c misc.c shf.c syn.c tree.c var.c
.if !make(test-build) .if !make(test-build)
. if ${DEBUGLIBS:L} == "yes" . if ${DEBUGLIBS:L} == "yes"
CPPFLAGS+= -DMKSH_AFREE_DEBUG # MirOS development version CPPFLAGS+= -DMKSH_AFREE_DEBUG # MirOS development version
#CPPFLAGS+= -DAALLOC_TRACK # MirOS development version
. endif . endif
CPPFLAGS+= -DAALLOC_NO_COOKIES # for now… aalloc cookies are broken
CPPFLAGS+= -DMKSH_ASSUME_UTF8 \ CPPFLAGS+= -DMKSH_ASSUME_UTF8 \
-DHAVE_ATTRIBUTE=1 -DHAVE_ATTRIBUTE_BOUNDED=1 \ -DHAVE_ATTRIBUTE=1 -DHAVE_ATTRIBUTE_BOUNDED=1 \
-DHAVE_ATTRIBUTE_USED=1 -DHAVE_SYS_PARAM_H=1 \ -DHAVE_ATTRIBUTE_USED=1 -DHAVE_SYS_PARAM_H=1 \
@ -29,8 +22,8 @@ CPPFLAGS+= -DMKSH_ASSUME_UTF8 \
-DHAVE_SYS_SIGNAME=1 -DHAVE_SYS_SIGLIST=1 -DHAVE_STRSIGNAL=0 \ -DHAVE_SYS_SIGNAME=1 -DHAVE_SYS_SIGLIST=1 -DHAVE_STRSIGNAL=0 \
-DHAVE_ARC4RANDOM=1 -DHAVE_ARC4RANDOM_PUSHB=1 -DHAVE_MKNOD=1 \ -DHAVE_ARC4RANDOM=1 -DHAVE_ARC4RANDOM_PUSHB=1 -DHAVE_MKNOD=1 \
-DHAVE_MKSTEMP=1 -DHAVE_NICE=1 -DHAVE_REALPATH=1 \ -DHAVE_MKSTEMP=1 -DHAVE_NICE=1 -DHAVE_REALPATH=1 \
-DHAVE_REVOKE=1 -DHAVE_SETLOCALE_CTYPE=1 \ -DHAVE_REVOKE=1 -DHAVE_SETLOCALE_CTYPE=0 \
-DHAVE_LANGINFO_CODESET=1 -DHAVE_SETMODE=1 \ -DHAVE_LANGINFO_CODESET=0 -DHAVE_SETMODE=1 \
-DHAVE_SETRESUGID=1 -DHAVE_SETGROUPS=1 -DHAVE_STRCASESTR=1 \ -DHAVE_SETRESUGID=1 -DHAVE_SETGROUPS=1 -DHAVE_STRCASESTR=1 \
-DHAVE_STRLCPY=1 -DHAVE_ARC4RANDOM_DECL=1 \ -DHAVE_STRLCPY=1 -DHAVE_ARC4RANDOM_DECL=1 \
-DHAVE_ARC4RANDOM_PUSHB_DECL=1 -DHAVE_FLOCK_DECL=1 \ -DHAVE_ARC4RANDOM_PUSHB_DECL=1 -DHAVE_FLOCK_DECL=1 \

33
alloc.c
View File

@ -29,51 +29,40 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.12 2008/11/15 07:35:23 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.13 2008/12/13 17:02:11 tg Exp $");
struct link { struct link {
struct link *prev; struct link *prev;
struct link *next; struct link *next;
}; };
struct TArea { Area *
struct link *freelist; /* free list */ ainit(Area *ap)
};
PArea
anew(size_t hint __unused)
{ {
PArea ap;
if ((ap = malloc(sizeof (struct TArea))) == NULL)
internal_errorf("unable to allocate memory");
ap->freelist = NULL; ap->freelist = NULL;
return (ap); return (ap);
} }
void void
adelete(PArea *pap) afreeall(Area *ap)
{ {
struct link *l, *l2; struct link *l, *l2;
for (l = (*pap)->freelist; l != NULL; l = l2) { for (l = ap->freelist; l != NULL; l = l2) {
l2 = l->next; l2 = l->next;
free(l); free(l);
} }
free(*pap); ap->freelist = NULL;
*pap = NULL;
} }
#define L2P(l) ( (void *)(((char *)(l)) + sizeof (struct link)) ) #define L2P(l) ( (void *)(((char *)(l)) + sizeof (struct link)) )
#define P2L(p) ( (struct link *)(((ptrdiff_t)(p)) - sizeof (struct link)) ) #define P2L(p) ( (struct link *)(((ptrdiff_t)(p)) - sizeof (struct link)) )
void * void *
alloc(size_t nmemb, size_t size, PArea ap) alloc(size_t size, Area *ap)
{ {
struct link *l; struct link *l;
size *= nmemb;
if ((l = malloc(sizeof (struct link) + size)) == NULL) if ((l = malloc(sizeof (struct link) + size)) == NULL)
internal_errorf("unable to allocate memory"); internal_errorf("unable to allocate memory");
l->next = ap->freelist; l->next = ap->freelist;
@ -86,14 +75,12 @@ alloc(size_t nmemb, size_t size, PArea ap)
} }
void * void *
aresize(void *ptr, size_t nmemb, size_t size, PArea ap) aresize(void *ptr, size_t size, Area *ap)
{ {
struct link *l, *l2, *lprev, *lnext; struct link *l, *l2, *lprev, *lnext;
if (ptr == NULL) if (ptr == NULL)
return (alloc(nmemb, size, ap)); return (alloc(size, ap));
size *= nmemb;
l = P2L(ptr); l = P2L(ptr);
lprev = l->prev; lprev = l->prev;
@ -112,7 +99,7 @@ aresize(void *ptr, size_t nmemb, size_t size, PArea ap)
} }
void void
afree(void *ptr, PArea ap) afree(void *ptr, Area *ap)
{ {
struct link *l; struct link *l;
#ifdef MKSH_AFREE_DEBUG #ifdef MKSH_AFREE_DEBUG

16
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.251 2008/12/08 13:57:35 tg Exp $ # $MirOS: src/bin/mksh/check.t,v 1.252 2008/12/13 17:02:11 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $ # $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -7,7 +7,7 @@
# http://www.research.att.com/~gsf/public/ifs.sh # http://www.research.att.com/~gsf/public/ifs.sh
expected-stdout: expected-stdout:
@(#)MIRBSD KSH R36 2008/12/04 @(#)MIRBSD KSH R36 2008/12/13
description: description:
Check version of shell. Check version of shell.
stdin: stdin:
@ -4483,8 +4483,8 @@ name: utf8bom-3
description: description:
Reading the UTF-8 BOM should enable the utf8-mode flag Reading the UTF-8 BOM should enable the utf8-mode flag
stdin: stdin:
"$__progname" -c ':; if [[ $(set +o) = *@(-o utf8-mode)@(| *) ]]; then print on; else print off; fi' "$__progname" -c ':; if [[ $- = *U* ]]; then print on; else print off; fi'
"$__progname" -c ':; if [[ $(set +o) = *@(-o utf8-mode)@(| *) ]]; then print on; else print off; fi' "$__progname" -c ':; if [[ $- = *U* ]]; then print on; else print off; fi'
expected-stdout: expected-stdout:
off off
on on
@ -4495,7 +4495,7 @@ description:
category: !os:hpux category: !os:hpux
env-setup: !PS1=!PS2=!LC_CTYPE=en_US.UTF-8! env-setup: !PS1=!PS2=!LC_CTYPE=en_US.UTF-8!
stdin: stdin:
if [[ $(set +o) = *@(-o utf8-mode)@(| *) ]]; then if [[ $- = *U* ]]; then
print is set print is set
else else
print is not set print is not set
@ -4509,7 +4509,7 @@ description:
category: os:hpux category: os:hpux
env-setup: !PS1=!PS2=!LC_CTYPE=en_US.utf8! env-setup: !PS1=!PS2=!LC_CTYPE=en_US.utf8!
stdin: stdin:
if [[ $(set +o) = *@(-o utf8-mode)@(| *) ]]; then if [[ $- = *U* ]]; then
print is set print is set
else else
print is not set print is not set
@ -4524,7 +4524,7 @@ category: !os:hpux
arguments: !-i! arguments: !-i!
env-setup: !PS1=!PS2=!LC_CTYPE=en_US.UTF-8! env-setup: !PS1=!PS2=!LC_CTYPE=en_US.UTF-8!
stdin: stdin:
if [[ $(set +o) = *@(-o utf8-mode)@(| *) ]]; then if [[ $- = *U* ]]; then
print is set print is set
else else
print is not set print is not set
@ -4541,7 +4541,7 @@ category: os:hpux
arguments: !-i! arguments: !-i!
env-setup: !PS1=!PS2=!LC_CTYPE=en_US.utf8! env-setup: !PS1=!PS2=!LC_CTYPE=en_US.utf8!
stdin: stdin:
if [[ $(set +o) = *@(-o utf8-mode)@(| *) ]]; then if [[ $- = *U* ]]; then
print is set print is set
else else
print is not set print is not set

View File

@ -1,4 +1,4 @@
$MirOS: src/bin/mksh/copyright,v 1.25 2008/11/12 05:00:40 tg Exp $ $MirOS: src/bin/mksh/copyright,v 1.26 2008/12/13 17:02:12 tg Exp $
The MirBSD Korn Shell (mksh) is The MirBSD Korn Shell (mksh) is
@ -28,5 +28,5 @@ tors. See the documentation, CVS, and web site for details.
A number of files that are either always or conditionally included A number of files that are either always or conditionally included
during compilation, depending on the target operating environment, during compilation, depending on the target operating environment,
are covered by different (2/3-clause BSD or similar) licences; re- are covered by different (2/3-clause BSD or similar) licences; re-
fer to the individual files for details: alloc.c (included by user fer to the individual files for details: alloc.c (always included)
request) and setmode.c, strlcpy.c (included when required) and setmode.c, strlcpy.c (included when required)

27
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.147 2008/12/04 18:11:04 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/edit.c,v 1.148 2008/12/13 17:02:12 tg Exp $");
/* tty driver characters we are interested in */ /* tty driver characters we are interested in */
typedef struct { typedef struct {
@ -400,7 +400,7 @@ x_command_glob(int flags, const char *str, int slen, char ***wordsp)
int i, path_order = 0; int i, path_order = 0;
info = (struct path_order_info *) info = (struct path_order_info *)
alloc(nwords, sizeof (struct path_order_info), ATEMP); alloc(nwords * sizeof (struct path_order_info), ATEMP);
for (i = 0; i < nwords; i++) { for (i = 0; i < nwords; i++) {
info[i].word = words[i]; info[i].word = words[i];
info[i].base = x_basename(words[i], NULL); info[i].base = x_basename(words[i], NULL);
@ -960,7 +960,8 @@ utf_wctomb(char *dst, unsigned int wc)
/* +++ emacs editing mode +++ */ /* +++ emacs editing mode +++ */
static PArea AEDIT; /* area for kill ring and macro defns */ static Area aedit;
#define AEDIT &aedit /* area for kill ring and macro defns */
#define MKCTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F) /* ASCII */ #define MKCTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F) /* ASCII */
#define UNCTRL(x) ((x) ^ 0x40) /* ASCII */ #define UNCTRL(x) ((x) ^ 0x40) /* ASCII */
@ -1071,7 +1072,7 @@ static int x_search(char *, int, int);
static int x_match(char *, char *); static int x_match(char *, char *);
static void x_redraw(int); static void x_redraw(int);
static void x_push(int); static void x_push(int);
static char *x_mapin(const char *, PArea); static char *x_mapin(const char *, Area *);
static char *x_mapout(int); static char *x_mapout(int);
static void x_mapout2(int, char **); static void x_mapout2(int, char **);
static void x_print(int, int); static void x_print(int, int);
@ -2517,7 +2518,7 @@ x_error(int c __unused)
} }
static char * static char *
x_mapin(const char *cp, PArea ap) x_mapin(const char *cp, Area *ap)
{ {
char *new, *op; char *new, *op;
@ -2681,10 +2682,10 @@ x_init_emacs(void)
{ {
int i, j; int i, j;
AEDIT = anew(8); ainit(AEDIT);
x_nextcmd = -1; x_nextcmd = -1;
x_tab = alloc(X_NTABS, sizeof (*x_tab), AEDIT); x_tab = alloc(X_NTABS * sizeof (*x_tab), AEDIT);
for (j = 0; j < X_TABSZ; j++) for (j = 0; j < X_TABSZ; j++)
x_tab[0][j] = XFUNC_insert; x_tab[0][j] = XFUNC_insert;
for (i = 1; i < X_NTABS; i++) for (i = 1; i < X_NTABS; i++)
@ -2694,7 +2695,7 @@ x_init_emacs(void)
x_tab[x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char] x_tab[x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char]
= x_defbindings[i].xdb_func; = x_defbindings[i].xdb_func;
x_atab = alloc(X_NTABS, sizeof (*x_atab), AEDIT); x_atab = alloc(X_NTABS * sizeof (*x_atab), AEDIT);
for (i = 1; i < X_NTABS; i++) for (i = 1; i < X_NTABS; i++)
for (j = 0; j < X_TABSZ; j++) for (j = 0; j < X_TABSZ; j++)
x_atab[i][j] = NULL; x_atab[i][j] = NULL;
@ -3600,8 +3601,8 @@ x_vi(char *buf, size_t len)
if (!wbuf_len || wbuf_len != x_cols - 3) { if (!wbuf_len || wbuf_len != x_cols - 3) {
wbuf_len = x_cols - 3; wbuf_len = x_cols - 3;
wbuf[0] = aresize(wbuf[0], 1, wbuf_len, APERM); wbuf[0] = aresize(wbuf[0], wbuf_len, APERM);
wbuf[1] = aresize(wbuf[1], 1, wbuf_len, APERM); wbuf[1] = aresize(wbuf[1], wbuf_len, APERM);
} }
(void)memset(wbuf[0], ' ', wbuf_len); (void)memset(wbuf[0], ' ', wbuf_len);
(void)memset(wbuf[1], ' ', wbuf_len); (void)memset(wbuf[1], ' ', wbuf_len);
@ -4130,7 +4131,7 @@ vi_cmd(int argcnt, const char *cmd)
nlen = strlen(ap->val.s) + 1; nlen = strlen(ap->val.s) + 1;
olen = !macro.p ? 2 : olen = !macro.p ? 2 :
macro.len - (macro.p - macro.buf); macro.len - (macro.p - macro.buf);
nbuf = alloc(1, nlen + 1 + olen, APERM); nbuf = alloc(nlen + 1 + olen, APERM);
memcpy(nbuf, ap->val.s, nlen); memcpy(nbuf, ap->val.s, nlen);
nbuf[nlen++] = cmd[1]; nbuf[nlen++] = cmd[1];
if (macro.p) { if (macro.p) {
@ -4777,8 +4778,8 @@ save_edstate(struct edstate *old)
{ {
struct edstate *new; struct edstate *new;
new = alloc(1, sizeof (struct edstate), APERM); new = alloc(sizeof (struct edstate), APERM);
new->cbuf = alloc(1, old->cbufsize, APERM); new->cbuf = alloc(old->cbufsize, APERM);
memcpy(new->cbuf, old->cbuf, old->linelen); memcpy(new->cbuf, old->cbuf, old->linelen);
new->cbufsize = old->cbufsize; new->cbufsize = old->cbufsize;
new->linelen = old->linelen; new->linelen = old->linelen;

11
eval.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.50 2008/11/12 00:54:47 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/eval.c,v 1.51 2008/12/13 17:02:13 tg Exp $");
#ifdef MKSH_SMALL #ifdef MKSH_SMALL
#define MKSH_NOPWNAM #define MKSH_NOPWNAM
@ -297,8 +297,7 @@ expand(const char *cp, /* input word */
if (!st->next) { if (!st->next) {
SubType *newst; SubType *newst;
newst = alloc(1, newst = alloc(sizeof (SubType), ATEMP);
sizeof (SubType), ATEMP);
newst->next = NULL; newst->next = NULL;
newst->prev = st; newst->prev = st;
st->next = newst; st->next = newst;
@ -590,7 +589,7 @@ expand(const char *cp, /* input word */
*/ */
len = strlen(dp) + 1; len = strlen(dp) + 1;
setstr(st->var, setstr(st->var,
debunk(alloc(1, len, ATEMP), debunk(alloc(len, ATEMP),
dp, len), KSH_UNWIND_ERROR); dp, len), KSH_UNWIND_ERROR);
x.str = str_val(st->var); x.str = str_val(st->var);
type = XSUB; type = XSUB;
@ -770,7 +769,7 @@ expand(const char *cp, /* input word */
Xlength(ds, dp) == 0) { Xlength(ds, dp) == 0) {
char *p; char *p;
*(p = alloc(1, 1, ATEMP)) = '\0'; *(p = alloc(1, ATEMP)) = '\0';
XPput(*wp, p); XPput(*wp, p);
} }
type = XSUBMID; type = XSUBMID;
@ -1474,7 +1473,7 @@ alt_expand(XPtrV *wp, char *start, char *exp_start, char *end, int fdo)
l1 = brace_start - start; l1 = brace_start - start;
l2 = (p - 1) - field_start; l2 = (p - 1) - field_start;
l3 = end - brace_end; l3 = end - brace_end;
new = alloc(1, l1 + l2 + l3 + 1, ATEMP); new = alloc(l1 + l2 + l3 + 1, ATEMP);
memcpy(new, start, l1); memcpy(new, start, l1);
memcpy(new + l1, field_start, l2); memcpy(new + l1, field_start, l2);
memcpy(new + l1 + l2, brace_end, l3); memcpy(new + l1 + l2, brace_end, l3);

4
exec.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.47 2008/11/12 00:55:31 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/exec.c,v 1.48 2008/12/13 17:02:13 tg Exp $");
static int comexec(struct op *, struct tbl *volatile, const char **, static int comexec(struct op *, struct tbl *volatile, const char **,
int volatile); int volatile);
@ -72,7 +72,7 @@ execute(struct op *volatile t,
flags &= ~XTIME; flags &= ~XTIME;
if (t->ioact != NULL || t->type == TPIPE || t->type == TCOPROC) { if (t->ioact != NULL || t->type == TPIPE || t->type == TCOPROC) {
e->savefd = alloc(NUFILE, sizeof (short), ATEMP); e->savefd = alloc(NUFILE * sizeof (short), ATEMP);
/* initialise to not redirected */ /* initialise to not redirected */
memset(e->savefd, 0, NUFILE * sizeof (short)); memset(e->savefd, 0, NUFILE * sizeof (short));
} }

4
expr.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/expr.c,v 1.20 2008/11/12 00:54:48 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/expr.c,v 1.21 2008/12/13 17:02:13 tg Exp $");
/* The order of these enums is constrained by the order of opinfo[] */ /* The order of these enums is constrained by the order of opinfo[] */
enum token { enum token {
@ -544,7 +544,7 @@ tempvar(void)
{ {
struct tbl *vp; struct tbl *vp;
vp = alloc(1, sizeof (struct tbl), ATEMP); vp = alloc(sizeof (struct tbl), ATEMP);
vp->flag = ISSET|INTEGER; vp->flag = ISSET|INTEGER;
vp->type = 0; vp->type = 0;
vp->areap = ATEMP; vp->areap = ATEMP;

10
funcs.c
View File

@ -5,7 +5,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.92 2008/11/12 00:54:48 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.93 2008/12/13 17:02:14 tg Exp $");
/* A leading = means assignments before command are kept; /* A leading = means assignments before command are kept;
* a leading * means a POSIX special builtin; * a leading * means a POSIX special builtin;
@ -202,7 +202,7 @@ c_cd(const char **wp)
olen = strlen(wp[0]); olen = strlen(wp[0]);
nlen = strlen(wp[1]); nlen = strlen(wp[1]);
elen = strlen(current_wd + ilen + olen) + 1; elen = strlen(current_wd + ilen + olen) + 1;
dir = allocd = alloc(1, ilen + nlen + elen, ATEMP); dir = allocd = alloc(ilen + nlen + elen, ATEMP);
memcpy(dir, current_wd, ilen); memcpy(dir, current_wd, ilen);
memcpy(dir + ilen, wp[1], nlen); memcpy(dir + ilen, wp[1], nlen);
memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen); memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen);
@ -2111,9 +2111,9 @@ c_set(const char **wp)
owp = wp += argi - 1; owp = wp += argi - 1;
wp[0] = l->argv[0]; /* save $0 */ wp[0] = l->argv[0]; /* save $0 */
while (*++wp != NULL) while (*++wp != NULL)
strdupx(*wp, *wp, l->areap); strdupx(*wp, *wp, &l->area);
l->argc = wp - owp - 1; l->argc = wp - owp - 1;
l->argv = alloc(l->argc + 2, sizeof (char *), l->areap); l->argv = alloc((l->argc + 2) * sizeof (char *), &l->area);
for (wp = l->argv; (*wp++ = *owp++) != NULL; ) for (wp = l->argv; (*wp++ = *owp++) != NULL; )
; ;
} }
@ -3064,7 +3064,7 @@ c_realpath(const char **wp)
else { else {
char *buf; char *buf;
if (realpath(*wp, (buf = alloc(1, PATH_MAX, ATEMP))) == NULL) { if (realpath(*wp, (buf = alloc(PATH_MAX, ATEMP))) == NULL) {
rv = errno; rv = errno;
bi_errorf("%s: %s", *wp, strerror(rv)); bi_errorf("%s: %s", *wp, strerror(rv));
} else } else

View File

@ -3,7 +3,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.75 2008/11/12 00:54:48 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.76 2008/12/13 17:02:14 tg Exp $");
/*- /*-
* MirOS: This is the default mapping type, and need not be specified. * MirOS: This is the default mapping type, and need not be specified.
@ -68,7 +68,7 @@ c_fc(const char **wp)
sflag++; sflag++;
else { else {
size_t len = strlen(p); size_t len = strlen(p);
editor = alloc(1, len + 4, ATEMP); editor = alloc(len + 4, ATEMP);
memcpy(editor, p, len); memcpy(editor, p, len);
memcpy(editor + len, " $_", 4); memcpy(editor + len, " $_", 4);
} }
@ -504,7 +504,7 @@ sethistsize(int n)
cursize = n; cursize = n;
} }
history = aresize(history, n, sizeof (char *), APERM); history = aresize(history, n * sizeof (char *), APERM);
histsize = n; histsize = n;
histptr = history + cursize; histptr = history + cursize;
@ -555,7 +555,7 @@ init_histvec(void)
{ {
if (history == (char **)NULL) { if (history == (char **)NULL) {
histsize = HISTORYSIZE; histsize = HISTORYSIZE;
history = alloc(histsize, sizeof (char *), APERM); history = alloc(histsize * sizeof (char *), APERM);
histptr = history - 1; histptr = history - 1;
} }
} }

6
jobs.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.41 2008/11/30 10:33:38 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.42 2008/12/13 17:02:15 tg Exp $");
/* Order important! */ /* Order important! */
#define PRUNNING 0 #define PRUNNING 0
@ -1399,7 +1399,7 @@ new_job(void)
newj = free_jobs; newj = free_jobs;
free_jobs = free_jobs->next; free_jobs = free_jobs->next;
} else } else
newj = alloc(1, sizeof (Job), APERM); newj = alloc(sizeof (Job), APERM);
/* brute force method */ /* brute force method */
for (i = 1; ; i++) { for (i = 1; ; i++) {
@ -1426,7 +1426,7 @@ new_proc(void)
p = free_procs; p = free_procs;
free_procs = free_procs->next; free_procs = free_procs->next;
} else } else
p = alloc(1, sizeof (Proc), APERM); p = alloc(sizeof (Proc), APERM);
return p; return p;
} }

12
lex.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.78 2008/12/04 18:11:05 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/lex.c,v 1.79 2008/12/13 17:02:15 tg Exp $");
/* /*
* states while lexing word * states while lexing word
@ -776,7 +776,7 @@ yylex(int cf)
dp = Xstring(ws, wp); dp = Xstring(ws, wp);
if ((c == '<' || c == '>' || c == '&') && state == SBASE) { if ((c == '<' || c == '>' || c == '&') && state == SBASE) {
struct ioword *iop = alloc(1, sizeof (struct ioword), ATEMP); struct ioword *iop = alloc(sizeof (struct ioword), ATEMP);
if (Xlength(ws, wp) == 0) if (Xlength(ws, wp) == 0)
iop->unit = c == '<' ? 0 : 1; iop->unit = c == '<' ? 0 : 1;
@ -1038,11 +1038,11 @@ yyerror(const char *fmt, ...)
*/ */
Source * Source *
pushs(int type, PArea areap) pushs(int type, Area *areap)
{ {
Source *s; Source *s;
s = alloc(1, sizeof (Source), areap); s = alloc(sizeof (Source), areap);
s->type = type; s->type = type;
s->str = null; s->str = null;
s->start = NULL; s->start = NULL;
@ -1268,7 +1268,7 @@ set_prompt(int to, Source *s)
{ {
struct shf *shf; struct shf *shf;
char * volatile ps1; char * volatile ps1;
PArea saved_atemp; Area *saved_atemp;
ps1 = str_val(global("PS1")); ps1 = str_val(global("PS1"));
shf = shf_sopen(NULL, strlen(ps1) * 2, shf = shf_sopen(NULL, strlen(ps1) * 2,
@ -1525,7 +1525,7 @@ getsc_bn(void)
static Lex_state * static Lex_state *
push_state_(State_info *si, Lex_state *old_end) push_state_(State_info *si, Lex_state *old_end)
{ {
Lex_state *new = alloc(STATE_BSIZE, sizeof (Lex_state), ATEMP); Lex_state *new = alloc(STATE_BSIZE * sizeof (Lex_state), ATEMP);
new[0].ls_info.base = old_end; new[0].ls_info.base = old_end;
si->base = &new[0]; si->base = &new[0];

40
main.c
View File

@ -13,7 +13,7 @@
#include <locale.h> #include <locale.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.115 2008/12/04 18:11:06 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/main.c,v 1.116 2008/12/13 17:02:15 tg Exp $");
extern char **environ; extern char **environ;
@ -22,7 +22,7 @@ extern uid_t kshuid;
extern gid_t kshgid, kshegid; extern gid_t kshgid, kshegid;
#endif #endif
static void reclaim(bool); static void reclaim(void);
static void remove_temps(struct temp *); static void remove_temps(struct temp *);
static const char initifs[] = "IFS= \t\n"; static const char initifs[] = "IFS= \t\n";
@ -91,11 +91,11 @@ main(int argc, const char *argv[])
} }
kshname = *argv; kshname = *argv;
APERM = anew(256); /* initialise permanent Area */ ainit(&aperm); /* initialise permanent Area */
/* set up base environment */ /* set up base environment */
env.type = E_NONE; env.type = E_NONE;
env.areap = anew(32); ainit(&env.area);
newblock(); /* set up global l->vars and l->funs */ newblock(); /* set up global l->vars and l->funs */
/* Do this first so output routines (eg, errorf, shellf) can work */ /* Do this first so output routines (eg, errorf, shellf) can work */
@ -135,7 +135,7 @@ main(int argc, const char *argv[])
#else #else
#ifdef _CS_PATH #ifdef _CS_PATH
if ((k = confstr(_CS_PATH, NULL, 0)) != (size_t)-1 && k > 0 && if ((k = confstr(_CS_PATH, NULL, 0)) != (size_t)-1 && k > 0 &&
confstr(_CS_PATH, cp = alloc(1, k + 1, APERM), k + 1) == k + 1) confstr(_CS_PATH, cp = alloc(k + 1, APERM), k + 1) == k + 1)
def_path = cp; def_path = cp;
else else
#endif #endif
@ -569,7 +569,7 @@ shell(Source * volatile s, volatile int toplevel)
if (t != NULL && t->type != TEOF && interactive && really_exit) if (t != NULL && t->type != TEOF && interactive && really_exit)
really_exit = 0; really_exit = 0;
reclaim(false); reclaim();
} }
quitenv(NULL); quitenv(NULL);
source = old_source; source = old_source;
@ -613,10 +613,10 @@ newenv(int type)
{ {
struct env *ep; struct env *ep;
ep = alloc(1, sizeof (struct env), ATEMP); ep = alloc(sizeof (struct env), ATEMP);
ep->type = type; ep->type = type;
ep->flags = 0; ep->flags = 0;
ep->areap = anew(16); ainit(&ep->area);
ep->loc = e->loc; ep->loc = e->loc;
ep->savefd = NULL; ep->savefd = NULL;
ep->oenv = e; ep->oenv = e;
@ -668,12 +668,12 @@ quitenv(struct shf *shf)
} }
if (shf) if (shf)
shf_close(shf); shf_close(shf);
reclaim(true); reclaim();
exit(exstat); exit(exstat);
} }
if (shf) if (shf)
shf_close(shf); shf_close(shf);
reclaim(true); reclaim();
e = e->oenv; e = e->oenv;
afree(ep, ATEMP); afree(ep, ATEMP);
@ -697,7 +697,7 @@ cleanup_parents_env(void)
for (fd = 0; fd < NUFILE; fd++) for (fd = 0; fd < NUFILE; fd++)
if (ep->savefd[fd] > 0) if (ep->savefd[fd] > 0)
close(ep->savefd[fd]); close(ep->savefd[fd]);
afree(ep->savefd, ep->areap); afree(ep->savefd, &ep->area);
ep->savefd = NULL; ep->savefd = NULL;
} }
} }
@ -716,13 +716,11 @@ cleanup_proc_env(void)
/* remove temp files and free ATEMP Area */ /* remove temp files and free ATEMP Area */
static void static void
reclaim(bool finish) reclaim(void)
{ {
remove_temps(e->temps); remove_temps(e->temps);
e->temps = NULL; e->temps = NULL;
adelete(&e->areap); afreeall(&e->area);
if (!finish)
e->areap = anew(16);
} }
static void static void
@ -1142,7 +1140,7 @@ coproc_cleanup(int reuse)
} }
struct temp * struct temp *
maketemp(PArea ap, Temp_type type, struct temp **tlist) maketemp(Area *ap, Temp_type type, struct temp **tlist)
{ {
struct temp *tp; struct temp *tp;
int len; int len;
@ -1157,7 +1155,7 @@ maketemp(PArea ap, Temp_type type, struct temp **tlist)
pathname = tempnam(dir, "mksh."); pathname = tempnam(dir, "mksh.");
len = ((pathname == NULL) ? 0 : strlen(pathname)) + 1; len = ((pathname == NULL) ? 0 : strlen(pathname)) + 1;
#endif #endif
tp = alloc(1, sizeof (struct temp) + len, ap); tp = alloc(sizeof (struct temp) + len, ap);
tp->name = (char *)&tp[1]; tp->name = (char *)&tp[1];
#if !HAVE_MKSTEMP #if !HAVE_MKSTEMP
if (pathname == NULL) if (pathname == NULL)
@ -1200,7 +1198,7 @@ hash(const char *n)
} }
void void
ktinit(struct table *tp, PArea ap, int tsize) ktinit(struct table *tp, Area *ap, int tsize)
{ {
tp->areap = ap; tp->areap = ap;
tp->tbls = NULL; tp->tbls = NULL;
@ -1217,7 +1215,7 @@ texpand(struct table *tp, int nsize)
struct tbl **ntblp, **otblp = tp->tbls; struct tbl **ntblp, **otblp = tp->tbls;
int osize = tp->size; int osize = tp->size;
ntblp = alloc(nsize, sizeof (struct tbl *), tp->areap); ntblp = alloc(nsize * sizeof (struct tbl *), tp->areap);
for (i = 0; i < nsize; i++) for (i = 0; i < nsize; i++)
ntblp[i] = NULL; ntblp[i] = NULL;
tp->size = nsize; tp->size = nsize;
@ -1290,7 +1288,7 @@ ktenter(struct table *tp, const char *n, unsigned int h)
} }
/* create new tbl entry */ /* create new tbl entry */
len = strlen(n) + 1; len = strlen(n) + 1;
p = alloc(1, offsetof(struct tbl, name[0]) + len, tp->areap); p = alloc(offsetof(struct tbl, name[0]) + len, tp->areap);
p->flag = 0; p->flag = 0;
p->type = 0; p->type = 0;
p->areap = tp->areap; p->areap = tp->areap;
@ -1337,7 +1335,7 @@ ktsort(struct table *tp)
size_t i; size_t i;
struct tbl **p, **sp, **dp; struct tbl **p, **sp, **dp;
p = alloc(tp->size + 1, sizeof (struct tbl *), ATEMP); p = alloc((tp->size + 1) * sizeof (struct tbl *), ATEMP);
sp = tp->tbls; /* source */ sp = tp->tbls; /* source */
dp = p; /* dest */ dp = p; /* dest */
for (i = 0; i < (size_t)tp->size; i++) for (i = 0; i < (size_t)tp->size; i++)

20
misc.c
View File

@ -6,7 +6,7 @@
#include <grp.h> #include <grp.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.92 2008/12/04 18:11:06 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/misc.c,v 1.93 2008/12/13 17:02:16 tg Exp $");
#undef USE_CHVT #undef USE_CHVT
#if defined(TIOCSCTTY) && !defined(MKSH_SMALL) #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; const char *old_beg = xsp->beg;
xsp->len += more > xsp->len ? more : xsp->len; xsp->len += more > xsp->len ? more : xsp->len;
xsp->beg = aresize(xsp->beg, 1, xsp->len + 8, xsp->areap); xsp->beg = aresize(xsp->beg, xsp->len + 8, xsp->areap);
xsp->end = xsp->beg + xsp->len; xsp->end = xsp->beg + xsp->len;
return xsp->beg + (xp - old_beg); return xsp->beg + (xp - old_beg);
} }
@ -493,7 +493,7 @@ gmatchx(const char *s, const char *p, bool isfile)
if (!isfile && !has_globbing(p, pe)) { if (!isfile && !has_globbing(p, pe)) {
size_t len = pe - p + 1; size_t len = pe - p + 1;
char tbuf[64]; char tbuf[64];
char *t = len <= sizeof(tbuf) ? tbuf : alloc(1, len, ATEMP); char *t = len <= sizeof(tbuf) ? tbuf : alloc(len, ATEMP);
debunk(t, p, len); debunk(t, p, len);
return !strcmp(t, s); return !strcmp(t, s);
} }
@ -933,7 +933,7 @@ print_columns(struct shf *shf, int n,
char *(*func) (const void *, int, char *, int), char *(*func) (const void *, int, char *, int),
const void *arg, int max_width, int prefcol) const void *arg, int max_width, int prefcol)
{ {
char *str = alloc(1, max_width + 1, ATEMP); char *str = alloc(max_width + 1, ATEMP);
int i, r, c, rows, cols, nspace; int i, r, c, rows, cols, nspace;
/* max_width + 1 for the space. Note that no space /* max_width + 1 for the space. Note that no space
@ -1048,8 +1048,8 @@ ksh_get_wd(size_t *dlen)
char *ret, *b; char *ret, *b;
size_t len = 1; size_t len = 1;
if ((ret = getcwd((b = alloc(1, PATH_MAX + 1, ATEMP)), PATH_MAX))) if ((ret = getcwd((b = alloc(PATH_MAX + 1, ATEMP)), PATH_MAX)))
ret = aresize(b, 1, len = (strlen(b) + 1), ATEMP); ret = aresize(b, len = (strlen(b) + 1), ATEMP);
else else
afree(b, ATEMP); afree(b, ATEMP);
@ -1229,7 +1229,7 @@ set_current_wd(char *pathl)
if (len > current_wd_size) { if (len > current_wd_size) {
afree(current_wd, APERM); afree(current_wd, APERM);
current_wd = alloc(1, current_wd_size = len, APERM); current_wd = alloc(current_wd_size = len, APERM);
} }
memcpy(current_wd, p, len); memcpy(current_wd, p, len);
if (p != pathl && p != null) if (p != pathl && p != null)
@ -1426,19 +1426,19 @@ stristr(const char *b, const char *l)
#ifdef MKSH_SMALL #ifdef MKSH_SMALL
char * char *
strndup_(const char *src, size_t len, PArea ap) strndup_(const char *src, size_t len, Area *ap)
{ {
char *dst = NULL; char *dst = NULL;
if (src != NULL) { if (src != NULL) {
dst = alloc(1, ++len, ap); dst = alloc(++len, ap);
strlcpy(dst, src, len); strlcpy(dst, src, len);
} }
return (dst); return (dst);
} }
char * char *
strdup_(const char *src, PArea ap) strdup_(const char *src, Area *ap)
{ {
return (src == NULL ? NULL : strndup_(src, strlen(src), ap)); return (src == NULL ? NULL : strndup_(src, strlen(src), ap));
} }

117
sh.h
View File

@ -103,9 +103,9 @@
#define __SCCSID(x) __IDSTRING(sccsid,x) #define __SCCSID(x) __IDSTRING(sccsid,x)
#ifdef EXTERN #ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.265 2008/12/04 18:11:08 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.266 2008/12/13 17:02:16 tg Exp $");
#endif #endif
#define MKSH_VERSION "R36 2008/12/04" #define MKSH_VERSION "R36 2008/12/13"
#ifndef MKSH_INCLUDES_ONLY #ifndef MKSH_INCLUDES_ONLY
@ -130,7 +130,10 @@ typedef int bool;
/* extra macros */ /* extra macros */
#ifndef timerclear #ifndef timerclear
#define timerclear(tvp) do { (tvp)->tv_sec = (tvp)->tv_usec = 0; } while (0) #define timerclear(tvp) \
do { \
(tvp)->tv_sec = (tvp)->tv_usec = 0; \
} while (/* CONSTCOND */ 0)
#endif #endif
#ifndef timeradd #ifndef timeradd
#define timeradd(tvp, uvp, vvp) \ #define timeradd(tvp, uvp, vvp) \
@ -141,7 +144,7 @@ typedef int bool;
(vvp)->tv_sec++; \ (vvp)->tv_sec++; \
(vvp)->tv_usec -= 1000000; \ (vvp)->tv_usec -= 1000000; \
} \ } \
} while (0) } while (/* CONSTCOND */ 0)
#endif #endif
#ifndef timersub #ifndef timersub
#define timersub(tvp, uvp, vvp) \ #define timersub(tvp, uvp, vvp) \
@ -152,7 +155,7 @@ typedef int bool;
(vvp)->tv_sec--; \ (vvp)->tv_sec--; \
(vvp)->tv_usec += 1000000; \ (vvp)->tv_usec += 1000000; \
} \ } \
} while (0) } while (/* CONSTCOND */ 0)
#endif #endif
#define ksh_isdigit(c) (((c) >= '0') && ((c) <= '9')) #define ksh_isdigit(c) (((c) >= '0') && ((c) <= '9'))
@ -363,7 +366,7 @@ char *ucstrstr(char *, const char *);
\ \
if (strdup_src != NULL) { \ if (strdup_src != NULL) { \
size_t strdup_len = strlen(strdup_src) + 1; \ size_t strdup_len = strlen(strdup_src) + 1; \
strdup_dst = alloc(1, strdup_len, (ap)); \ strdup_dst = alloc(strdup_len, (ap)); \
strlcpy(strdup_dst, strdup_src, strdup_len); \ strlcpy(strdup_dst, strdup_src, strdup_len); \
} \ } \
(d) = strdup_dst; \ (d) = strdup_dst; \
@ -374,7 +377,7 @@ char *ucstrstr(char *, const char *);
\ \
if (strdup_src != NULL) { \ if (strdup_src != NULL) { \
size_t strdup_len = (n) + 1; \ size_t strdup_len = (n) + 1; \
strdup_dst = alloc(1, strdup_len, (ap)); \ strdup_dst = alloc(strdup_len, (ap)); \
strlcpy(strdup_dst, strdup_src, strdup_len); \ strlcpy(strdup_dst, strdup_src, strdup_len); \
} \ } \
(d) = strdup_dst; \ (d) = strdup_dst; \
@ -389,15 +392,22 @@ char *ucstrstr(char *, const char *);
#define MKSH_NOVI #define MKSH_NOVI
#endif #endif
typedef struct TArea *PArea; /*
EXTERN PArea APERM; /* permanent object space */ * Area-based allocation built on malloc/free
#define ATEMP e->areap */
typedef struct Area {
struct link *freelist; /* free list */
} Area;
EXTERN Area aperm; /* permanent object space */
#define APERM &aperm
#define ATEMP &e->area
/* /*
* parsing & execution environment * parsing & execution environment
*/ */
extern struct env { extern struct env {
PArea areap; /* temporary allocation area */ Area area; /* temporary allocation area */
struct block *loc; /* local variables and functions */ struct block *loc; /* local variables and functions */
short *savefd; /* original redirected fds */ short *savefd; /* original redirected fds */
struct env *oenv; /* link to previous environment */ struct env *oenv; /* link to previous environment */
@ -746,13 +756,13 @@ struct shf {
int fd; /* file descriptor */ int fd; /* file descriptor */
int errno_; /* saved value of errno after error */ int errno_; /* saved value of errno after error */
int bsize; /* actual size of buf */ int bsize; /* actual size of buf */
PArea areap; /* area shf/buf were allocated in */ Area *areap; /* area shf/buf were allocated in */
}; };
extern struct shf shf_iob[]; extern struct shf shf_iob[];
struct table { struct table {
PArea areap; /* area to allocate entries */ Area *areap; /* area to allocate entries */
struct tbl **tbls; /* hashed table items */ struct tbl **tbls; /* hashed table items */
short size, nfree; /* hash size (always 2^^n), free entries */ short size, nfree; /* hash size (always 2^^n), free entries */
}; };
@ -761,7 +771,7 @@ struct tbl { /* table item */
Tflag flag; /* flags */ Tflag flag; /* flags */
int type; /* command type (see below), base (if INTEGER), int type; /* command type (see below), base (if INTEGER),
* or offset from val.s of value (if EXPORT) */ * or offset from val.s of value (if EXPORT) */
PArea areap; /* area to allocate from */ Area *areap; /* area to allocate from */
union { union {
char *s; /* string */ char *s; /* string */
long i; /* integer */ long i; /* integer */
@ -853,7 +863,7 @@ struct arg_info {
* activation record for function blocks * activation record for function blocks
*/ */
struct block { struct block {
PArea areap; /* area to allocate things */ Area area; /* area to allocate things */
const char **argv; const char **argv;
int argc; int argc;
int flags; /* see BF_* */ int flags; /* see BF_* */
@ -1057,7 +1067,7 @@ struct ioword {
typedef struct XString { typedef struct XString {
char *end, *beg; /* end, begin of string */ char *end, *beg; /* end, begin of string */
size_t len; /* length */ size_t len; /* length */
PArea areap; /* area to allocate/free from */ Area *areap; /* area to allocate/free from */
} XString; } XString;
typedef char *XStringP; typedef char *XStringP;
@ -1066,23 +1076,23 @@ typedef char *XStringP;
#define XinitN(xs, length, area) do { \ #define XinitN(xs, length, area) do { \
(xs).len = (length); \ (xs).len = (length); \
(xs).areap = (area); \ (xs).areap = (area); \
(xs).beg = alloc(1, (xs).len + X_EXTRA, (xs).areap); \ (xs).beg = alloc((xs).len + X_EXTRA, (xs).areap); \
(xs).end = (xs).beg + (xs).len; \ (xs).end = (xs).beg + (xs).len; \
} while (0) } while (/* CONSTCOND */ 0)
#define Xinit(xs, xp, length, area) do { \ #define Xinit(xs, xp, length, area) do { \
XinitN((xs), (length), (area)); \ XinitN((xs), (length), (area)); \
(xp) = (xs).beg; \ (xp) = (xs).beg; \
} while (0) } while (/* CONSTCOND */ 0)
/* stuff char into string */ /* stuff char into string */
#define Xput(xs, xp, c) (*xp++ = (c)) #define Xput(xs, xp, c) (*xp++ = (c))
/* check if there are at least n bytes left */ /* check if there are at least n bytes left */
#define XcheckN(xs, xp, n) do { \ #define XcheckN(xs, xp, n) do { \
int more = ((xp) + (n)) - (xs).end; \ size_t more = ((xp) + (n)) - (xs).end; \
if (more > 0) \ if (more > 0) \
(xp) = Xcheck_grow_(&(xs), (xp), more); \ (xp) = Xcheck_grow_(&(xs), (xp), more); \
} while (0) } while (/* CONSTCOND */ 0)
/* check for overflow, expand string */ /* check for overflow, expand string */
#define Xcheck(xs, xp) XcheckN((xs), (xp), 1) #define Xcheck(xs, xp) XcheckN((xs), (xp), 1)
@ -1091,16 +1101,16 @@ typedef char *XStringP;
#define Xfree(xs, xp) afree((xs).beg, (xs).areap) #define Xfree(xs, xp) afree((xs).beg, (xs).areap)
/* close, return string */ /* close, return string */
#define Xclose(xs, xp) aresize((void*)(xs).beg, 1, \ #define Xclose(xs, xp) aresize((xs).beg, (xp) - (xs).beg, (xs).areap)
(size_t)((xp) - (xs).beg), (xs).areap)
/* begin of string */ /* begin of string */
#define Xstring(xs, xp) ((xs).beg) #define Xstring(xs, xp) ((xs).beg)
#define Xnleft(xs, xp) ((xs).end - (xp)) /* may be less than 0 */ #define Xnleft(xs, xp) ((xs).end - (xp)) /* may be less than 0 */
#define Xlength(xs, xp) ((xp) - (xs).beg) #define Xlength(xs, xp) ((xp) - (xs).beg)
#define Xsize(xs, xp) ((xs).end - (xs).beg) #define Xsize(xs, xp) ((xs).end - (xs).beg)
#define Xsavepos(xs, xp) ((xp) - (xs).beg) #define Xsavepos(xs, xp) ((xp) - (xs).beg)
#define Xrestpos(xs, xp, n) ((xs).beg + (n)) #define Xrestpos(xs, xp, n) ((xs).beg + (n))
char *Xcheck_grow_(XString *, const char *, unsigned int); char *Xcheck_grow_(XString *, const char *, unsigned int);
@ -1115,28 +1125,25 @@ typedef struct XPtrV {
#define XPinit(x, n) do { \ #define XPinit(x, n) do { \
void **vp__; \ void **vp__; \
vp__ = alloc((n), sizeof (void *), ATEMP); \ vp__ = alloc((n) * sizeof (void *), ATEMP); \
(x).cur = (x).beg = vp__; \ (x).cur = (x).beg = vp__; \
(x).end = vp__ + (n); \ (x).end = vp__ + (n); \
} while (0) } while (/* CONSTCOND */ 0)
#define XPput(x, p) do { \ #define XPput(x, p) do { \
if ((x).cur >= (x).end) { \ if ((x).cur >= (x).end) { \
size_t n = XPsize(x); \ size_t n = XPsize(x); \
(x).beg = aresize((void*)(x).beg, \ (x).beg = aresize((x).beg, \
n * 2, sizeof (void *), ATEMP); \ n * 2 * sizeof (void *), ATEMP); \
(x).cur = (x).beg + n; \ (x).cur = (x).beg + n; \
(x).end = (x).cur + n; \ (x).end = (x).cur + n; \
} \ } \
*(x).cur++ = (p); \ *(x).cur++ = (p); \
} while (0) } while (/* CONSTCOND */ 0)
#define XPptrv(x) ((x).beg) #define XPptrv(x) ((x).beg)
#define XPsize(x) ((x).cur - (x).beg) #define XPsize(x) ((x).cur - (x).beg)
#define XPclose(x) aresize((x).beg, XPsize(x) * sizeof (void *), ATEMP)
#define XPclose(x) aresize((void*)(x).beg, \
XPsize(x), sizeof (void *), ATEMP)
#define XPfree(x) afree((x).beg, ATEMP) #define XPfree(x) afree((x).beg, ATEMP)
#define IDENT 64 #define IDENT 64
@ -1156,7 +1163,7 @@ struct source {
int errline; /* line the error occurred on (0 if not set) */ int errline; /* line the error occurred on (0 if not set) */
const char *file; /* input file name */ const char *file; /* input file name */
int flags; /* SF_* */ int flags; /* SF_* */
PArea areap; Area *areap;
XString xs; /* input buffer */ XString xs; /* input buffer */
Source *next; /* stacked source */ Source *next; /* stacked source */
char ugbuf[2]; /* buffer for ungetsc() (SREREAD) and char ugbuf[2]; /* buffer for ungetsc() (SREREAD) and
@ -1250,17 +1257,11 @@ EXTERN int histsize; /* history size */
EXTERN struct timeval j_usrtime, j_systime; EXTERN struct timeval j_usrtime, j_systime;
/* alloc.c */ /* alloc.c */
#ifdef AALLOC_STATS Area *ainit(Area *);
#define AALLOC_STRINGIFY(x) __STRING(x) void afreeall(Area *);
#define anew(hint) anewEx((hint), __FILE__ ":" AALLOC_STRINGIFY(__LINE__)) void *alloc(size_t, Area *); /* cannot fail */
PArea anewEx(size_t, const char *); /* cannot fail */ void *aresize(void *, size_t, Area *);
#else void afree(void *, Area *); /* can take NULL */
PArea anew(size_t); /* cannot fail */
#endif
void adelete(PArea *);
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 */ /* edit.c */
void x_init(void); void x_init(void);
int x_read(char *, size_t); int x_read(char *, size_t);
@ -1391,7 +1392,7 @@ int yylex(int);
void yyerror(const char *, ...) void yyerror(const char *, ...)
__attribute__((noreturn)) __attribute__((noreturn))
__attribute__((format (printf, 1, 2))); __attribute__((format (printf, 1, 2)));
Source *pushs(int, PArea); Source *pushs(int, Area *);
void set_prompt(int, Source *); void set_prompt(int, Source *);
void pprompt(const char *, int); void pprompt(const char *, int);
int promptlen(const char *); int promptlen(const char *);
@ -1440,12 +1441,12 @@ void coproc_readw_close(int);
void coproc_write_close(int); void coproc_write_close(int);
int coproc_getfd(int, const char **); int coproc_getfd(int, const char **);
void coproc_cleanup(int); void coproc_cleanup(int);
struct temp *maketemp(PArea, Temp_type, struct temp **); struct temp *maketemp(Area *, Temp_type, struct temp **);
unsigned int hash(const char *); unsigned int hash(const char *);
void ktinit(struct table *, PArea, int); void ktinit(struct table *, Area *, int);
struct tbl *ktsearch(struct table *, const char *, unsigned int); struct tbl *ktsearch(struct table *, const char *, unsigned int);
struct tbl *ktenter(struct table *, const char *, unsigned int); struct tbl *ktenter(struct table *, const char *, unsigned int);
#define ktdelete(p) do { p->flag = 0; } while (0) #define ktdelete(p) do { p->flag = 0; } while (/* CONSTCOND */ 0)
void ktwalk(struct tstate *, struct table *); void ktwalk(struct tstate *, struct table *);
struct tbl *ktnext(struct tstate *); struct tbl *ktnext(struct tstate *);
struct tbl **ktsort(struct table *); struct tbl **ktsort(struct table *);
@ -1478,8 +1479,8 @@ void simplify_path(char *);
char *get_phys_path(const char *); char *get_phys_path(const char *);
void set_current_wd(char *); void set_current_wd(char *);
#ifdef MKSH_SMALL #ifdef MKSH_SMALL
char *strdup_(const char *, PArea); char *strdup_(const char *, Area *);
char *strndup_(const char *, size_t, PArea); char *strndup_(const char *, size_t, Area *);
#endif #endif
/* shf.c */ /* shf.c */
struct shf *shf_open(const char *, int, int, int); struct shf *shf_open(const char *, int, int, int);
@ -1512,11 +1513,11 @@ struct op *compile(Source *);
/* tree.c */ /* tree.c */
int fptreef(struct shf *, int, const char *, ...); int fptreef(struct shf *, int, const char *, ...);
char *snptreef(char *, int, const char *, ...); char *snptreef(char *, int, const char *, ...);
struct op *tcopy(struct op *, PArea); struct op *tcopy(struct op *, Area *);
char *wdcopy(const char *, PArea); char *wdcopy(const char *, Area *);
const char *wdscan(const char *, int); const char *wdscan(const char *, int);
char *wdstrip(const char *, bool, bool); char *wdstrip(const char *, bool, bool);
void tfree(struct op *, PArea); void tfree(struct op *, Area *);
/* var.c */ /* var.c */
void newblock(void); void newblock(void);
void popblock(void); void popblock(void);

14
shf.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.23 2008/11/12 00:54:51 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/shf.c,v 1.24 2008/12/13 17:02:17 tg Exp $");
/* flags to shf_emptybuf() */ /* flags to shf_emptybuf() */
#define EB_READSW 0x01 /* about to switch to reading */ #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; int fd;
/* Done before open so if alloca fails, fd won't be lost. */ /* Done before open so if alloca fails, fd won't be lost. */
shf = alloc(1, sizeof (struct shf) + bsize, ATEMP); shf = alloc(sizeof (struct shf) + bsize, ATEMP);
shf->areap = ATEMP; shf->areap = ATEMP;
shf->buf = (unsigned char *)&shf[1]; shf->buf = (unsigned char *)&shf[1];
shf->bsize = bsize; shf->bsize = bsize;
@ -92,12 +92,12 @@ shf_fdopen(int fd, int sflags, struct shf *shf)
if (shf) { if (shf) {
if (bsize) { if (bsize) {
shf->buf = alloc(1, bsize, ATEMP); shf->buf = alloc(bsize, ATEMP);
sflags |= SHF_ALLOCB; sflags |= SHF_ALLOCB;
} else } else
shf->buf = NULL; shf->buf = NULL;
} else { } else {
shf = alloc(1, sizeof (struct shf) + bsize, ATEMP); shf = alloc(sizeof (struct shf) + bsize, ATEMP);
shf->buf = (unsigned char *)&shf[1]; shf->buf = (unsigned char *)&shf[1];
sflags |= SHF_ALLOCS; 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); internal_errorf("shf_sopen: flags 0x%x", sflags);
if (!shf) { if (!shf) {
shf = alloc(1, sizeof (struct shf), ATEMP); shf = alloc(sizeof (struct shf), ATEMP);
sflags |= SHF_ALLOCS; sflags |= SHF_ALLOCS;
} }
shf->areap = ATEMP; shf->areap = ATEMP;
@ -188,7 +188,7 @@ shf_sopen(char *buf, int bsize, int sflags, struct shf *shf)
if (bsize <= 0) if (bsize <= 0)
bsize = 64; bsize = 64;
sflags |= SHF_ALLOCB; sflags |= SHF_ALLOCB;
buf = alloc(1, bsize, shf->areap); buf = alloc(bsize, shf->areap);
} }
shf->fd = -1; shf->fd = -1;
shf->buf = shf->rp = shf->wp = (unsigned char *)buf; shf->buf = shf->rp = shf->wp = (unsigned char *)buf;
@ -323,7 +323,7 @@ shf_emptybuf(struct shf *shf, int flags)
!(shf->flags & SHF_ALLOCB)) !(shf->flags & SHF_ALLOCB))
return EOF; return EOF;
/* allocate more space for buffer */ /* allocate more space for buffer */
nbuf = aresize(shf->buf, 2, shf->wbsize, shf->areap); nbuf = aresize(shf->buf, 2 * shf->wbsize, shf->areap);
shf->rp = nbuf + (shf->rp - shf->buf); shf->rp = nbuf + (shf->rp - shf->buf);
shf->wp = nbuf + (shf->wp - shf->buf); shf->wp = nbuf + (shf->wp - shf->buf);
shf->rbsize += shf->wbsize; shf->rbsize += shf->wbsize;

20
syn.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.31 2008/12/02 13:20:40 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/syn.c,v 1.32 2008/12/13 17:02:17 tg Exp $");
struct nesting_state { struct nesting_state {
int start_token; /* token than began nesting (eg, FOR) */ int start_token; /* token than began nesting (eg, FOR) */
@ -172,8 +172,8 @@ synio(int cf)
if (iop->flag & IOBASH) { if (iop->flag & IOBASH) {
char *cp; char *cp;
nextiop = alloc(1, sizeof (*iop), ATEMP); nextiop = alloc(sizeof (*iop), ATEMP);
nextiop->name = cp = alloc(5, 1, ATEMP); nextiop->name = cp = alloc(5, ATEMP);
if (iop->unit > 9) { if (iop->unit > 9) {
*cp++ = CHAR; *cp++ = CHAR;
@ -214,7 +214,7 @@ get_command(int cf)
XPtrV args, vars; XPtrV args, vars;
struct nesting_state old_nesting; struct nesting_state old_nesting;
iops = alloc(NUFILE + 1, sizeof (struct ioword *), ATEMP); iops = alloc((NUFILE + 1) * sizeof (struct ioword *), ATEMP);
XPinit(args, 16); XPinit(args, 16);
XPinit(vars, 16); XPinit(vars, 16);
@ -421,7 +421,7 @@ get_command(int cf)
syniocf &= ~(KEYWORD|ALIAS); syniocf &= ~(KEYWORD|ALIAS);
t = pipeline(0); t = pipeline(0);
if (t) { if (t) {
t->str = alloc(1, 2, ATEMP); t->str = alloc(2, ATEMP);
t->str[0] = '\0'; /* TF_* flags */ t->str[0] = '\0'; /* TF_* flags */
t->str[1] = '\0'; t->str[1] = '\0';
} }
@ -445,7 +445,7 @@ get_command(int cf)
t->ioact = NULL; t->ioact = NULL;
} else { } else {
iops[iopn++] = NULL; iops[iopn++] = NULL;
iops = aresize(iops, iopn, sizeof (struct ioword *), ATEMP); iops = aresize(iops, iopn * sizeof (struct ioword *), ATEMP);
t->ioact = iops; t->ioact = iops;
} }
@ -620,13 +620,13 @@ function_body(char *name,
* be used as input), we pretend there is a colon here. * be used as input), we pretend there is a colon here.
*/ */
t->left = newtp(TCOM); t->left = newtp(TCOM);
t->left->args = alloc(2, sizeof (char *), ATEMP); t->left->args = alloc(2 * sizeof (char *), ATEMP);
t->left->args[0] = tv = alloc(3, sizeof (char), ATEMP); t->left->args[0] = tv = alloc(3, ATEMP);
tv[0] = CHAR; tv[0] = CHAR;
tv[1] = ':'; tv[1] = ':';
tv[2] = EOS; tv[2] = EOS;
t->left->args[1] = NULL; t->left->args[1] = NULL;
t->left->vars = alloc(1, sizeof (char *), ATEMP); t->left->vars = alloc(sizeof (char *), ATEMP);
t->left->vars[0] = NULL; t->left->vars[0] = NULL;
t->left->lineno = 1; t->left->lineno = 1;
} }
@ -803,7 +803,7 @@ newtp(int type)
{ {
struct op *t; struct op *t;
t = alloc(1, sizeof (struct op), ATEMP); t = alloc(sizeof (struct op), ATEMP);
t->type = type; t->type = type;
t->u.evalflags = 0; t->u.evalflags = 0;
t->args = NULL; t->args = NULL;

28
tree.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.23 2008/11/12 00:54:51 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/tree.c,v 1.24 2008/12/13 17:02:17 tg Exp $");
#define INDENT 4 #define INDENT 4
@ -12,8 +12,8 @@ static void pioact(struct shf *, int, struct ioword *);
static void tputC(int, struct shf *); static void tputC(int, struct shf *);
static void tputS(char *, struct shf *); static void tputS(char *, struct shf *);
static void vfptreef(struct shf *, int, const char *, va_list); static void vfptreef(struct shf *, int, const char *, va_list);
static struct ioword **iocopy(struct ioword **, PArea); static struct ioword **iocopy(struct ioword **, Area *);
static void iofree(struct ioword **, PArea); static void iofree(struct ioword **, Area *);
/* /*
* print a command tree * print a command tree
@ -412,7 +412,7 @@ vfptreef(struct shf *shf, int indent, const char *fmt, va_list va)
* copy tree (for function definition) * copy tree (for function definition)
*/ */
struct op * struct op *
tcopy(struct op *t, PArea ap) tcopy(struct op *t, Area *ap)
{ {
struct op *r; struct op *r;
const char **tw; const char **tw;
@ -421,7 +421,7 @@ tcopy(struct op *t, PArea ap)
if (t == NULL) if (t == NULL)
return NULL; return NULL;
r = alloc(1, sizeof (struct op), ap); r = alloc(sizeof (struct op), ap);
r->type = t->type; r->type = t->type;
r->u.evalflags = t->u.evalflags; r->u.evalflags = t->u.evalflags;
@ -436,7 +436,7 @@ tcopy(struct op *t, PArea ap)
else { else {
for (tw = (const char **)t->vars; *tw++ != NULL; ) for (tw = (const char **)t->vars; *tw++ != NULL; )
; ;
rw = r->vars = alloc((tw - (const char **)t->vars + 1), rw = r->vars = alloc((tw - (const char **)t->vars + 1) *
sizeof (*tw), ap); sizeof (*tw), ap);
for (tw = (const char **)t->vars; *tw != NULL; ) for (tw = (const char **)t->vars; *tw != NULL; )
*rw++ = wdcopy(*tw++, ap); *rw++ = wdcopy(*tw++, ap);
@ -448,7 +448,7 @@ tcopy(struct op *t, PArea ap)
else { else {
for (tw = t->args; *tw++ != NULL; ) for (tw = t->args; *tw++ != NULL; )
; ;
r->args = (const char **)(rw = alloc((tw - t->args + 1), r->args = (const char **)(rw = alloc((tw - t->args + 1) *
sizeof (*tw), ap)); sizeof (*tw), ap));
for (tw = t->args; *tw != NULL; ) for (tw = t->args; *tw != NULL; )
*rw++ = wdcopy(*tw++, ap); *rw++ = wdcopy(*tw++, ap);
@ -465,10 +465,10 @@ tcopy(struct op *t, PArea ap)
} }
char * char *
wdcopy(const char *wp, PArea ap) wdcopy(const char *wp, Area *ap)
{ {
size_t len = wdscan(wp, EOS) - wp; size_t len = wdscan(wp, EOS) - wp;
return memcpy(alloc(1, len, ap), wp, len); return memcpy(alloc(len, ap), wp, len);
} }
/* return the position of prefix c in wp plus 1 */ /* return the position of prefix c in wp plus 1 */
@ -611,20 +611,20 @@ wdstrip(const char *wp, bool keepq, bool make_magic)
} }
static struct ioword ** static struct ioword **
iocopy(struct ioword **iow, PArea ap) iocopy(struct ioword **iow, Area *ap)
{ {
struct ioword **ior; struct ioword **ior;
int i; int i;
for (ior = iow; *ior++ != NULL; ) for (ior = iow; *ior++ != NULL; )
; ;
ior = alloc((ior - iow + 1), sizeof (struct ioword *), ap); ior = alloc((ior - iow + 1) * sizeof (struct ioword *), ap);
for (i = 0; iow[i] != NULL; i++) { for (i = 0; iow[i] != NULL; i++) {
struct ioword *p, *q; struct ioword *p, *q;
p = iow[i]; p = iow[i];
q = alloc(1, sizeof (struct ioword), ap); q = alloc(sizeof (struct ioword), ap);
ior[i] = q; ior[i] = q;
*q = *p; *q = *p;
if (p->name != NULL) if (p->name != NULL)
@ -643,7 +643,7 @@ iocopy(struct ioword **iow, PArea ap)
* free tree (for function definition) * free tree (for function definition)
*/ */
void void
tfree(struct op *t, PArea ap) tfree(struct op *t, Area *ap)
{ {
char **w; char **w;
@ -678,7 +678,7 @@ tfree(struct op *t, PArea ap)
} }
static void static void
iofree(struct ioword **iow, PArea ap) iofree(struct ioword **iow, Area *ap)
{ {
struct ioword **iop; struct ioword **iop;
struct ioword *p; struct ioword *p;

18
var.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.64 2008/12/04 18:11:08 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/var.c,v 1.65 2008/12/13 17:02:18 tg Exp $");
/* /*
* Variables * Variables
@ -37,9 +37,9 @@ newblock(void)
struct block *l; struct block *l;
static const char *empty[] = { null }; static const char *empty[] = { null };
l = alloc(1, sizeof (struct block), ATEMP); l = alloc(sizeof (struct block), ATEMP);
l->flags = 0; l->flags = 0;
l->areap = anew(Flag(FTALKING) ? 1024 : 64); ainit(&l->area); /* todo: could use e->area (l->area => l->areap) */
if (!e->loc) { if (!e->loc) {
l->argc = 0; l->argc = 0;
l->argv = empty; l->argv = empty;
@ -48,8 +48,8 @@ newblock(void)
l->argv = e->loc->argv; l->argv = e->loc->argv;
} }
l->exit = l->error = NULL; l->exit = l->error = NULL;
ktinit(&l->vars, l->areap, 0); ktinit(&l->vars, &l->area, 0);
ktinit(&l->funs, l->areap, 0); ktinit(&l->funs, &l->area, 0);
l->next = e->loc; l->next = e->loc;
e->loc = l; e->loc = l;
} }
@ -74,7 +74,7 @@ popblock(void)
} }
if (l->flags & BF_DOGETOPTS) if (l->flags & BF_DOGETOPTS)
user_opt = l->getopts_state; user_opt = l->getopts_state;
adelete(&l->areap); afreeall(&l->area);
afree(l, ATEMP); afree(l, ATEMP);
} }
@ -523,7 +523,7 @@ formatstr(struct tbl *vp, const char *s)
} else } else
nlen = olen; nlen = olen;
p = alloc(1, (psiz = nlen * /* MB_LEN_MAX */ 3 + 1), ATEMP); p = alloc((psiz = nlen * /* MB_LEN_MAX */ 3 + 1), ATEMP);
if (vp->flag & (RJUST|LJUST)) { if (vp->flag & (RJUST|LJUST)) {
int slen = olen, i = 0; int slen = olen, i = 0;
@ -595,7 +595,7 @@ export(struct tbl *vp, const char *val)
int vallen = strlen(val) + 1; int vallen = strlen(val) + 1;
vp->flag |= ALLOC; vp->flag |= ALLOC;
xp = alloc(1, namelen + 1 + vallen, vp->areap); xp = alloc(namelen + 1 + vallen, vp->areap);
memcpy(vp->val.s = xp, vp->name, namelen); memcpy(vp->val.s = xp, vp->name, namelen);
xp += namelen; xp += namelen;
*xp++ = '='; *xp++ = '=';
@ -1212,7 +1212,7 @@ arraysearch(struct tbl *vp, uint32_t val)
else else
new = curr; new = curr;
} else } else
new = alloc(1, sizeof (struct tbl) + namelen, vp->areap); new = alloc(sizeof (struct tbl) + namelen, vp->areap);
strlcpy(new->name, vp->name, namelen); strlcpy(new->name, vp->name, namelen);
new->flag = vp->flag & ~(ALLOC|DEFINED|ISSET|SPECIAL); new->flag = vp->flag & ~(ALLOC|DEFINED|ISSET|SPECIAL);
new->type = vp->type; new->type = vp->type;