isolate all knowledge of the area-based allocator from the rest of the code

cost: bss -= (0, 0, 16, 16); text += (520, 504, 516, 480)
[ gcc,pcc X full,small ]
This commit is contained in:
tg 2008-11-12 00:27:57 +00:00
parent c80c28633b
commit 246b762af7
6 changed files with 44 additions and 41 deletions

19
alloc.c
View File

@ -29,32 +29,39 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.9 2008/11/11 23:50:28 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.10 2008/11/12 00:27:54 tg Exp $");
struct link {
struct link *prev;
struct link *next;
};
struct TArea aperm;
struct TArea {
struct link *freelist; /* free list */
};
PArea
ainit(PArea ap)
anew(void)
{
PArea ap;
if ((ap = malloc(sizeof (struct TArea))) == NULL)
internal_errorf("unable to allocate memory");
ap->freelist = NULL;
return (ap);
}
void
afreeall(PArea ap)
adelete(PArea *pap)
{
struct link *l, *l2;
for (l = ap->freelist; l != NULL; l = l2) {
for (l = (*pap)->freelist; l != NULL; l = l2) {
l2 = l->next;
free(l);
}
ap->freelist = NULL;
free(*pap);
*pap = NULL;
}
#define L2P(l) ( (void *)(((char *)(l)) + sizeof (struct link)) )

7
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.142 2008/11/11 23:50:28 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.143 2008/11/12 00:27:54 tg Exp $");
/* tty driver characters we are interested in */
typedef struct {
@ -960,8 +960,7 @@ utf_wctomb(char *dst, unsigned int wc)
/* +++ emacs editing mode +++ */
static struct TArea aedit;
#define AEDIT &aedit /* area for kill ring and macro defns */
static PArea AEDIT; /* area for kill ring and macro defns */
#define MKCTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F) /* ASCII */
#define UNCTRL(x) ((x) ^ 0x40) /* ASCII */
@ -2682,7 +2681,7 @@ x_init_emacs(void)
{
int i, j;
ainit(AEDIT);
AEDIT = anew();
x_nextcmd = -1;
x_tab = (unsigned char (*)[X_TABSZ])alloc(sizeofN(*x_tab, X_NTABS), AEDIT);

View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.90 2008/10/28 14:32:40 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.91 2008/11/12 00:27:55 tg Exp $");
/* A leading = means assignments before command are kept;
* a leading * means a POSIX special builtin;
@ -2111,10 +2111,10 @@ c_set(const char **wp)
owp = wp += argi - 1;
wp[0] = l->argv[0]; /* save $0 */
while (*++wp != NULL)
strdupx(*wp, *wp, &l->area);
strdupx(*wp, *wp, l->areap);
l->argc = wp - owp - 1;
l->argv = (const char **)alloc(sizeofN(char *, l->argc+2),
&l->area);
l->areap);
for (wp = l->argv; (*wp++ = *owp++) != NULL; )
;
}

24
main.c
View File

@ -13,7 +13,7 @@
#include <locale.h>
#endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.107 2008/11/11 23:50:30 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.108 2008/11/12 00:27:56 tg Exp $");
extern char **environ;
@ -22,7 +22,7 @@ extern uid_t kshuid;
extern gid_t kshgid, kshegid;
#endif
static void reclaim(void);
static void reclaim(bool);
static void remove_temps(struct temp *);
static const char initifs[] = "IFS= \t\n";
@ -87,11 +87,11 @@ main(int argc, const char *argv[])
}
kshname = *argv;
ainit(&aperm); /* initialise permanent Area */
APERM = anew(); /* initialise permanent Area */
/* set up base environment */
env.type = E_NONE;
ainit(&env.area);
env.areap = anew();
newblock(); /* set up global l->vars and l->funs */
/* Do this first so output routines (eg, errorf, shellf) can work */
@ -563,7 +563,7 @@ shell(Source * volatile s, volatile int toplevel)
if (t != NULL && t->type != TEOF && interactive && really_exit)
really_exit = 0;
reclaim();
reclaim(false);
}
quitenv(NULL);
source = old_source;
@ -610,7 +610,7 @@ newenv(int type)
ep = (struct env *)alloc(sizeof (*ep), ATEMP);
ep->type = type;
ep->flags = 0;
ainit(&ep->area);
ep->areap = anew();
ep->loc = e->loc;
ep->savefd = NULL;
ep->oenv = e;
@ -662,12 +662,12 @@ quitenv(struct shf *shf)
}
if (shf)
shf_close(shf);
reclaim();
reclaim(true);
exit(exstat);
}
if (shf)
shf_close(shf);
reclaim();
reclaim(true);
e = e->oenv;
afree(ep, ATEMP);
@ -691,7 +691,7 @@ cleanup_parents_env(void)
for (fd = 0; fd < NUFILE; fd++)
if (ep->savefd[fd] > 0)
close(ep->savefd[fd]);
afree(ep->savefd, &ep->area);
afree(ep->savefd, ep->areap);
ep->savefd = NULL;
}
}
@ -710,11 +710,13 @@ cleanup_proc_env(void)
/* remove temp files and free ATEMP Area */
static void
reclaim(void)
reclaim(bool finish)
{
remove_temps(e->temps);
e->temps = NULL;
afreeall(&e->area);
adelete(&e->areap);
if (!finish)
e->areap = anew();
}
static void

19
sh.h
View File

@ -103,7 +103,7 @@
#define __SCCSID(x) __IDSTRING(sccsid,x)
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.253 2008/11/11 23:50:30 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.254 2008/11/12 00:27:56 tg Exp $");
#endif
#define MKSH_VERSION "R36 2008/11/10"
@ -389,20 +389,15 @@ char *ucstrstr(char *, const char *);
#define MKSH_NOVI
#endif
struct TArea {
struct link *freelist; /* free list */
};
typedef struct TArea *PArea;
extern struct TArea aperm; /* permanent object space */
#define APERM &aperm
#define ATEMP &e->area
EXTERN PArea APERM; /* permanent object space */
#define ATEMP e->areap
/*
* parsing & execution environment
*/
extern struct env {
struct TArea area; /* temporary allocation area */
PArea areap; /* temporary allocation area */
struct block *loc; /* local variables and functions */
short *savefd; /* original redirected fds */
struct env *oenv; /* link to previous environment */
@ -857,7 +852,7 @@ struct arg_info {
* activation record for function blocks
*/
struct block {
struct TArea area; /* area to allocate things */
PArea areap; /* area to allocate things */
const char **argv;
int argc;
int flags; /* see BF_* */
@ -1253,8 +1248,8 @@ EXTERN int histsize; /* history size */
EXTERN struct timeval j_usrtime, j_systime;
/* alloc.c */
PArea ainit(PArea);
void afreeall(PArea);
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 */

10
var.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.58 2008/10/28 14:32:43 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.59 2008/11/12 00:27:57 tg Exp $");
/*
* Variables
@ -39,7 +39,7 @@ newblock(void)
l = (struct block *)alloc(sizeof (struct block), ATEMP);
l->flags = 0;
ainit(&l->area); /* todo: could use e->area (l->area => l->areap) */
l->areap = anew(); /* TODO: could use e->area */
if (!e->loc) {
l->argc = 0;
l->argv = empty;
@ -48,8 +48,8 @@ newblock(void)
l->argv = e->loc->argv;
}
l->exit = l->error = NULL;
ktinit(&l->vars, &l->area, 0);
ktinit(&l->funs, &l->area, 0);
ktinit(&l->vars, l->areap, 0);
ktinit(&l->funs, l->areap, 0);
l->next = e->loc;
e->loc = l;
}
@ -74,7 +74,7 @@ popblock(void)
}
if (l->flags & BF_DOGETOPTS)
user_opt = l->getopts_state;
afreeall(&l->area);
adelete(&l->areap);
afree(l, ATEMP);
}