diff options
author | jilles <jilles@FreeBSD.org> | 2010-12-27 22:18:27 +0000 |
---|---|---|
committer | jilles <jilles@FreeBSD.org> | 2010-12-27 22:18:27 +0000 |
commit | f6812a9bf2e3848d63c02126ebcf725221fc8fff (patch) | |
tree | 4eb97f94782e379552bd3c47f3c0116d4b7abcbb | |
parent | 7d9fa04b1acb73718093971c279bd3dc314c6810 (diff) | |
download | FreeBSD-src-f6812a9bf2e3848d63c02126ebcf725221fc8fff.zip FreeBSD-src-f6812a9bf2e3848d63c02126ebcf725221fc8fff.tar.gz |
sh: Simplify "stack string" code slightly.
Maintain a pointer to the end of the stack string area instead of how much
space is left. This simplifies the macros in memalloc.h. The places where
the new variable must be updated are only where the memory area is created,
destroyed or resized.
-rw-r--r-- | bin/sh/memalloc.c | 21 | ||||
-rw-r--r-- | bin/sh/memalloc.h | 22 |
2 files changed, 17 insertions, 26 deletions
diff --git a/bin/sh/memalloc.c b/bin/sh/memalloc.c index 2571a7e..3ee104c 100644 --- a/bin/sh/memalloc.c +++ b/bin/sh/memalloc.c @@ -127,7 +127,7 @@ static struct stack_block *stackp; static struct stackmark *markp; char *stacknxt; int stacknleft; -int sstrnleft; +char *sstrend; static void @@ -146,6 +146,7 @@ stnewblock(int nbytes) sp->prev = stackp; stacknxt = SPACE(sp); stacknleft = allocsize - (stacknxt - (char*)sp); + sstrend = stacknxt + stacknleft; stackp = sp; INTON; } @@ -204,6 +205,7 @@ popstackmark(struct stackmark *mark) } stacknxt = mark->stacknxt; stacknleft = mark->stacknleft; + sstrend = stacknxt + stacknleft; INTON; } @@ -250,6 +252,7 @@ growstackblock(int min) stackp = sp; stacknxt = SPACE(sp); stacknleft = newlen - (stacknxt - (char*)sp); + sstrend = stacknxt + stacknleft; /* * Stack marks pointing to the start of the old block @@ -306,7 +309,6 @@ static char * growstrstackblock(int n, int min) { growstackblock(min); - sstrnleft = stackblocksize() - n; return stackblock() + n; } @@ -325,31 +327,20 @@ growstackstr(void) */ char * -makestrspace(int min) +makestrspace(int min, char *p) { int len; - len = stackblocksize() - sstrnleft; + len = p - stackblock(); return (growstrstackblock(len, min)); } - -void -ungrabstackstr(char *s, char *p) -{ - stacknleft += stacknxt - s; - stacknxt = s; - sstrnleft = stacknleft - (p - s); -} - - char * stputbin(const char *data, int len, char *p) { CHECKSTRSPACE(len, p); memcpy(p, data, len); - sstrnleft -= len; return (p + len); } diff --git a/bin/sh/memalloc.h b/bin/sh/memalloc.h index fcdd5ba..f3e8829 100644 --- a/bin/sh/memalloc.h +++ b/bin/sh/memalloc.h @@ -45,7 +45,7 @@ struct stackmark { extern char *stacknxt; extern int stacknleft; -extern int sstrnleft; +extern char *sstrend; pointer ckmalloc(size_t); pointer ckrealloc(pointer, int); @@ -57,8 +57,7 @@ void setstackmark(struct stackmark *); void popstackmark(struct stackmark *); void grabstackblock(int); char *growstackstr(void); -char *makestrspace(int); -void ungrabstackstr(char *, char *); +char *makestrspace(int, char *); char *stputbin(const char *data, int len, char *p); char *stputs(const char *data, char *p); @@ -66,10 +65,10 @@ char *stputs(const char *data, char *p); #define stackblock() stacknxt #define stackblocksize() stacknleft -#define STARTSTACKSTR(p) p = stackblock(), sstrnleft = stackblocksize() -#define STPUTC(c, p) (--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), --sstrnleft, *p++ = (c))) -#define CHECKSTRSPACE(n, p) { if (sstrnleft < n) p = makestrspace(n); } -#define USTPUTC(c, p) (--sstrnleft, *p++ = (c)) +#define STARTSTACKSTR(p) p = stackblock() +#define STPUTC(c, p) do { if (p == sstrend) p = growstackstr(); *p++ = (c); } while(0) +#define CHECKSTRSPACE(n, p) { if (sstrend - p < n) p = makestrspace(n, p); } +#define USTPUTC(c, p) (*p++ = (c)) /* * STACKSTRNUL's use is where we want to be able to turn a stack * (non-sentinel, character counting string) into a C string, @@ -77,10 +76,11 @@ char *stputs(const char *data, char *p); * Note: Because of STACKSTRNUL's semantics, STACKSTRNUL cannot be used * on a stack that will grabstackstr()ed. */ -#define STACKSTRNUL(p) (sstrnleft == 0? (p = growstackstr(), *p = '\0') : (*p = '\0')) -#define STUNPUTC(p) (++sstrnleft, --p) +#define STACKSTRNUL(p) (p == sstrend ? (p = growstackstr(), *p = '\0') : (*p = '\0')) +#define STUNPUTC(p) (--p) #define STTOPC(p) p[-1] -#define STADJUST(amount, p) (p += (amount), sstrnleft -= (amount)) -#define grabstackstr(p) stalloc(stackblocksize() - sstrnleft) +#define STADJUST(amount, p) (p += (amount)) +#define grabstackstr(p) stalloc((char *)p - stackblock()) +#define ungrabstackstr(s, p) stunalloc((s)) #define STPUTBIN(s, len, p) p = stputbin((s), (len), p) #define STPUTS(s, p) p = stputs((s), p) |