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
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=216743
@ -127,7 +127,7 @@ static struct stack_block *stackp;
|
|||||||
static struct stackmark *markp;
|
static struct stackmark *markp;
|
||||||
char *stacknxt;
|
char *stacknxt;
|
||||||
int stacknleft;
|
int stacknleft;
|
||||||
int sstrnleft;
|
char *sstrend;
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -146,6 +146,7 @@ stnewblock(int nbytes)
|
|||||||
sp->prev = stackp;
|
sp->prev = stackp;
|
||||||
stacknxt = SPACE(sp);
|
stacknxt = SPACE(sp);
|
||||||
stacknleft = allocsize - (stacknxt - (char*)sp);
|
stacknleft = allocsize - (stacknxt - (char*)sp);
|
||||||
|
sstrend = stacknxt + stacknleft;
|
||||||
stackp = sp;
|
stackp = sp;
|
||||||
INTON;
|
INTON;
|
||||||
}
|
}
|
||||||
@ -204,6 +205,7 @@ popstackmark(struct stackmark *mark)
|
|||||||
}
|
}
|
||||||
stacknxt = mark->stacknxt;
|
stacknxt = mark->stacknxt;
|
||||||
stacknleft = mark->stacknleft;
|
stacknleft = mark->stacknleft;
|
||||||
|
sstrend = stacknxt + stacknleft;
|
||||||
INTON;
|
INTON;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -250,6 +252,7 @@ growstackblock(int min)
|
|||||||
stackp = sp;
|
stackp = sp;
|
||||||
stacknxt = SPACE(sp);
|
stacknxt = SPACE(sp);
|
||||||
stacknleft = newlen - (stacknxt - (char*)sp);
|
stacknleft = newlen - (stacknxt - (char*)sp);
|
||||||
|
sstrend = stacknxt + stacknleft;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Stack marks pointing to the start of the old block
|
* Stack marks pointing to the start of the old block
|
||||||
@ -306,7 +309,6 @@ static char *
|
|||||||
growstrstackblock(int n, int min)
|
growstrstackblock(int n, int min)
|
||||||
{
|
{
|
||||||
growstackblock(min);
|
growstackblock(min);
|
||||||
sstrnleft = stackblocksize() - n;
|
|
||||||
return stackblock() + n;
|
return stackblock() + n;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,31 +327,20 @@ growstackstr(void)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
char *
|
char *
|
||||||
makestrspace(int min)
|
makestrspace(int min, char *p)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
len = stackblocksize() - sstrnleft;
|
len = p - stackblock();
|
||||||
return (growstrstackblock(len, min));
|
return (growstrstackblock(len, min));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
ungrabstackstr(char *s, char *p)
|
|
||||||
{
|
|
||||||
stacknleft += stacknxt - s;
|
|
||||||
stacknxt = s;
|
|
||||||
sstrnleft = stacknleft - (p - s);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
char *
|
char *
|
||||||
stputbin(const char *data, int len, char *p)
|
stputbin(const char *data, int len, char *p)
|
||||||
{
|
{
|
||||||
CHECKSTRSPACE(len, p);
|
CHECKSTRSPACE(len, p);
|
||||||
memcpy(p, data, len);
|
memcpy(p, data, len);
|
||||||
sstrnleft -= len;
|
|
||||||
return (p + len);
|
return (p + len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ struct stackmark {
|
|||||||
|
|
||||||
extern char *stacknxt;
|
extern char *stacknxt;
|
||||||
extern int stacknleft;
|
extern int stacknleft;
|
||||||
extern int sstrnleft;
|
extern char *sstrend;
|
||||||
|
|
||||||
pointer ckmalloc(size_t);
|
pointer ckmalloc(size_t);
|
||||||
pointer ckrealloc(pointer, int);
|
pointer ckrealloc(pointer, int);
|
||||||
@ -57,8 +57,7 @@ void setstackmark(struct stackmark *);
|
|||||||
void popstackmark(struct stackmark *);
|
void popstackmark(struct stackmark *);
|
||||||
void grabstackblock(int);
|
void grabstackblock(int);
|
||||||
char *growstackstr(void);
|
char *growstackstr(void);
|
||||||
char *makestrspace(int);
|
char *makestrspace(int, char *);
|
||||||
void ungrabstackstr(char *, char *);
|
|
||||||
char *stputbin(const char *data, int len, char *p);
|
char *stputbin(const char *data, int len, char *p);
|
||||||
char *stputs(const char *data, char *p);
|
char *stputs(const char *data, char *p);
|
||||||
|
|
||||||
@ -66,10 +65,10 @@ char *stputs(const char *data, char *p);
|
|||||||
|
|
||||||
#define stackblock() stacknxt
|
#define stackblock() stacknxt
|
||||||
#define stackblocksize() stacknleft
|
#define stackblocksize() stacknleft
|
||||||
#define STARTSTACKSTR(p) p = stackblock(), sstrnleft = stackblocksize()
|
#define STARTSTACKSTR(p) p = stackblock()
|
||||||
#define STPUTC(c, p) (--sstrnleft >= 0? (*p++ = (c)) : (p = growstackstr(), --sstrnleft, *p++ = (c)))
|
#define STPUTC(c, p) do { if (p == sstrend) p = growstackstr(); *p++ = (c); } while(0)
|
||||||
#define CHECKSTRSPACE(n, p) { if (sstrnleft < n) p = makestrspace(n); }
|
#define CHECKSTRSPACE(n, p) { if (sstrend - p < n) p = makestrspace(n, p); }
|
||||||
#define USTPUTC(c, p) (--sstrnleft, *p++ = (c))
|
#define USTPUTC(c, p) (*p++ = (c))
|
||||||
/*
|
/*
|
||||||
* STACKSTRNUL's use is where we want to be able to turn a stack
|
* STACKSTRNUL's use is where we want to be able to turn a stack
|
||||||
* (non-sentinel, character counting string) into a C string,
|
* (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
|
* Note: Because of STACKSTRNUL's semantics, STACKSTRNUL cannot be used
|
||||||
* on a stack that will grabstackstr()ed.
|
* on a stack that will grabstackstr()ed.
|
||||||
*/
|
*/
|
||||||
#define STACKSTRNUL(p) (sstrnleft == 0? (p = growstackstr(), *p = '\0') : (*p = '\0'))
|
#define STACKSTRNUL(p) (p == sstrend ? (p = growstackstr(), *p = '\0') : (*p = '\0'))
|
||||||
#define STUNPUTC(p) (++sstrnleft, --p)
|
#define STUNPUTC(p) (--p)
|
||||||
#define STTOPC(p) p[-1]
|
#define STTOPC(p) p[-1]
|
||||||
#define STADJUST(amount, p) (p += (amount), sstrnleft -= (amount))
|
#define STADJUST(amount, p) (p += (amount))
|
||||||
#define grabstackstr(p) stalloc(stackblocksize() - sstrnleft)
|
#define grabstackstr(p) stalloc((char *)p - stackblock())
|
||||||
|
#define ungrabstackstr(s, p) stunalloc((s))
|
||||||
#define STPUTBIN(s, len, p) p = stputbin((s), (len), p)
|
#define STPUTBIN(s, len, p) p = stputbin((s), (len), p)
|
||||||
#define STPUTS(s, p) p = stputs((s), p)
|
#define STPUTS(s, p) p = stputs((s), p)
|
||||||
|
Loading…
Reference in New Issue
Block a user