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" #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 {
struct link *prev; struct link *prev;
struct link *next; struct link *next;
}; };
struct TArea aperm; struct TArea {
struct link *freelist; /* free list */
};
PArea PArea
ainit(PArea ap) anew(void)
{ {
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
afreeall(PArea ap) adelete(PArea *pap)
{ {
struct link *l, *l2; struct link *l, *l2;
for (l = ap->freelist; l != NULL; l = l2) { for (l = (*pap)->freelist; l != NULL; l = l2) {
l2 = l->next; l2 = l->next;
free(l); free(l);
} }
ap->freelist = NULL; free(*pap);
*pap = NULL;
} }
#define L2P(l) ( (void *)(((char *)(l)) + sizeof (struct link)) ) #define L2P(l) ( (void *)(((char *)(l)) + sizeof (struct link)) )

7
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h" #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 */ /* tty driver characters we are interested in */
typedef struct { typedef struct {
@ -960,8 +960,7 @@ utf_wctomb(char *dst, unsigned int wc)
/* +++ emacs editing mode +++ */ /* +++ emacs editing mode +++ */
static struct TArea aedit; static PArea AEDIT; /* area for kill ring and macro defns */
#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 */
@ -2682,7 +2681,7 @@ x_init_emacs(void)
{ {
int i, j; int i, j;
ainit(AEDIT); AEDIT = anew();
x_nextcmd = -1; x_nextcmd = -1;
x_tab = (unsigned char (*)[X_TABSZ])alloc(sizeofN(*x_tab, X_NTABS), AEDIT); x_tab = (unsigned char (*)[X_TABSZ])alloc(sizeofN(*x_tab, X_NTABS), AEDIT);

View File

@ -5,7 +5,7 @@
#include "sh.h" #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 assignments before command are kept;
* a leading * means a POSIX special builtin; * a leading * means a POSIX special builtin;
@ -2111,10 +2111,10 @@ 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->area); strdupx(*wp, *wp, l->areap);
l->argc = wp - owp - 1; l->argc = wp - owp - 1;
l->argv = (const char **)alloc(sizeofN(char *, l->argc+2), l->argv = (const char **)alloc(sizeofN(char *, l->argc+2),
&l->area); l->areap);
for (wp = l->argv; (*wp++ = *owp++) != NULL; ) for (wp = l->argv; (*wp++ = *owp++) != NULL; )
; ;
} }

24
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.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; 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(void); static void reclaim(bool);
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";
@ -87,11 +87,11 @@ main(int argc, const char *argv[])
} }
kshname = *argv; kshname = *argv;
ainit(&aperm); /* initialise permanent Area */ APERM = anew(); /* initialise permanent Area */
/* set up base environment */ /* set up base environment */
env.type = E_NONE; env.type = E_NONE;
ainit(&env.area); env.areap = anew();
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 */
@ -563,7 +563,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(); reclaim(false);
} }
quitenv(NULL); quitenv(NULL);
source = old_source; source = old_source;
@ -610,7 +610,7 @@ newenv(int type)
ep = (struct env *)alloc(sizeof (*ep), ATEMP); ep = (struct env *)alloc(sizeof (*ep), ATEMP);
ep->type = type; ep->type = type;
ep->flags = 0; ep->flags = 0;
ainit(&ep->area); ep->areap = anew();
ep->loc = e->loc; ep->loc = e->loc;
ep->savefd = NULL; ep->savefd = NULL;
ep->oenv = e; ep->oenv = e;
@ -662,12 +662,12 @@ quitenv(struct shf *shf)
} }
if (shf) if (shf)
shf_close(shf); shf_close(shf);
reclaim(); reclaim(true);
exit(exstat); exit(exstat);
} }
if (shf) if (shf)
shf_close(shf); shf_close(shf);
reclaim(); reclaim(true);
e = e->oenv; e = e->oenv;
afree(ep, ATEMP); afree(ep, ATEMP);
@ -691,7 +691,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->area); afree(ep->savefd, ep->areap);
ep->savefd = NULL; ep->savefd = NULL;
} }
} }
@ -710,11 +710,13 @@ cleanup_proc_env(void)
/* remove temp files and free ATEMP Area */ /* remove temp files and free ATEMP Area */
static void static void
reclaim(void) reclaim(bool finish)
{ {
remove_temps(e->temps); remove_temps(e->temps);
e->temps = NULL; e->temps = NULL;
afreeall(&e->area); adelete(&e->areap);
if (!finish)
e->areap = anew();
} }
static void static void

19
sh.h
View File

@ -103,7 +103,7 @@
#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.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 #endif
#define MKSH_VERSION "R36 2008/11/10" #define MKSH_VERSION "R36 2008/11/10"
@ -389,20 +389,15 @@ char *ucstrstr(char *, const char *);
#define MKSH_NOVI #define MKSH_NOVI
#endif #endif
struct TArea {
struct link *freelist; /* free list */
};
typedef struct TArea *PArea; typedef struct TArea *PArea;
EXTERN PArea APERM; /* permanent object space */
extern struct TArea aperm; /* permanent object space */ #define ATEMP e->areap
#define APERM &aperm
#define ATEMP &e->area
/* /*
* parsing & execution environment * parsing & execution environment
*/ */
extern struct env { extern struct env {
struct TArea area; /* temporary allocation area */ PArea areap; /* 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 */
@ -857,7 +852,7 @@ struct arg_info {
* activation record for function blocks * activation record for function blocks
*/ */
struct block { struct block {
struct TArea area; /* area to allocate things */ PArea areap; /* area to allocate things */
const char **argv; const char **argv;
int argc; int argc;
int flags; /* see BF_* */ int flags; /* see BF_* */
@ -1253,8 +1248,8 @@ EXTERN int histsize; /* history size */
EXTERN struct timeval j_usrtime, j_systime; EXTERN struct timeval j_usrtime, j_systime;
/* alloc.c */ /* alloc.c */
PArea ainit(PArea); PArea anew(void); /* cannot fail */
void afreeall(PArea); void adelete(PArea *);
void *alloc(size_t, PArea); /* cannot fail */ void *alloc(size_t, PArea); /* cannot fail */
void *aresize(void *, size_t, PArea); void *aresize(void *, size_t, PArea);
void afree(void *, PArea); /* can take NULL */ void afree(void *, PArea); /* can take NULL */

10
var.c
View File

@ -2,7 +2,7 @@
#include "sh.h" #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 * Variables
@ -39,7 +39,7 @@ newblock(void)
l = (struct block *)alloc(sizeof (struct block), ATEMP); l = (struct block *)alloc(sizeof (struct block), ATEMP);
l->flags = 0; 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) { 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->area, 0); ktinit(&l->vars, l->areap, 0);
ktinit(&l->funs, &l->area, 0); ktinit(&l->funs, l->areap, 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;
afreeall(&l->area); adelete(&l->areap);
afree(l, ATEMP); afree(l, ATEMP);
} }