6 #define X_EXTRA 8 /* this many extra bytes in X string */
12 Xinit(xs
, xp
, 128, ATEMP
); /* allocate initial string */
13 while ((c
= generate()) {
14 Xcheck(xs
, xp
); /* expand string if neccessary */
15 Xput(xs
, xp
, c
); /* add character */
17 return Xclose(xs
, xp
); /* resize string */
20 * The Xcheck and Xinit macros have a magic + X_EXTRA in the lengths.
21 * This is so that you can put up to X_EXTRA characters in a XString
22 * before calling Xcheck. (See yylex in lex.c)
26 typedef struct XString
{
27 char *end
, *beg
; /* end, begin of string */
28 size_t len
; /* length */
29 Area
*areap
; /* area to allocate/free from */
32 typedef char * XStringP
;
34 /* initialize expandable string */
35 #define Xinit(xs, xp, length, area) do { \
37 (xs).areap = (area); \
38 (xs).beg = alloc((xs).len + X_EXTRA, (xs).areap); \
39 (xs).end = (xs).beg + (xs).len; \
43 /* stuff char into string */
44 #define Xput(xs, xp, c) (*xp++ = (c))
46 /* check if there are at least n bytes left */
47 #define XcheckN(xs, xp, n) do { \
48 int more = ((xp) + (n)) - (xs).end; \
50 xp = Xcheck_grow_(&xs, xp, more); \
53 /* check for overflow, expand string */
54 #define Xcheck(xs, xp) XcheckN(xs, xp, 1)
57 #define Xfree(xs, xp) afree((void*) (xs).beg, (xs).areap)
59 /* close, return string */
60 #define Xclose(xs, xp) (char*) aresize((void*)(xs).beg, \
61 (size_t)((xp) - (xs).beg), (xs).areap)
63 #define Xstring(xs, xp) ((xs).beg)
65 #define Xnleft(xs, xp) ((xs).end - (xp)) /* may be less than 0 */
66 #define Xlength(xs, xp) ((xp) - (xs).beg)
67 #define Xsize(xs, xp) ((xs).end - (xs).beg)
68 #define Xsavepos(xs, xp) ((xp) - (xs).beg)
69 #define Xrestpos(xs, xp, n) ((xs).beg + (n))
71 char * Xcheck_grow_ (XString
*xsp
, const char *, unsigned);
74 * expandable vector of generic pointers
77 typedef struct XPtrV
{
78 void **cur
; /* next avail pointer */
79 void **beg
, **end
; /* begin, end of vector */
82 #define XPinit(x, n) do { \
83 register void **vp__; \
84 vp__ = (void**) alloc(sizeofN(void*, n), ATEMP); \
85 (x).cur = (x).beg = vp__; \
89 #define XPput(x, p) do { \
90 if ((x).cur >= (x).end) { \
92 (x).beg = (void**) aresize((void*) (x).beg, \
93 sizeofN(void*, n*2), ATEMP); \
94 (x).cur = (x).beg + n; \
95 (x).end = (x).cur + n; \
100 #define XPptrv(x) ((x).beg)
101 #define XPsize(x) ((x).cur - (x).beg)
103 #define XPclose(x) (void**) aresize((void*)(x).beg, \
104 sizeofN(void*, XPsize(x)), ATEMP)
106 #define XPfree(x) afree((void*) (x).beg, ATEMP)