summaryrefslogtreecommitdiffstats
path: root/bin/sh/memalloc.h
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2010-12-27 22:18:27 +0000
committerjilles <jilles@FreeBSD.org>2010-12-27 22:18:27 +0000
commitf6812a9bf2e3848d63c02126ebcf725221fc8fff (patch)
tree4eb97f94782e379552bd3c47f3c0116d4b7abcbb /bin/sh/memalloc.h
parent7d9fa04b1acb73718093971c279bd3dc314c6810 (diff)
downloadFreeBSD-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.
Diffstat (limited to 'bin/sh/memalloc.h')
-rw-r--r--bin/sh/memalloc.h22
1 files changed, 11 insertions, 11 deletions
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)
OpenPOWER on IntegriCloud