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.
This commit is contained in:
parent
eab54f6a13
commit
ff802dc7bb
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user