1 /* $NetBSD: expand.h,v 1.5 2013/10/18 19:53:34 christos Exp $ */
6 /* $Id: expand.h,v 1.5 2013/10/18 19:53:34 christos Exp $ */
8 #define X_EXTRA 8 /* this many extra bytes in X string */
14 Xinit(xs
, xp
, 128, ATEMP
); /* allocate initial string */
15 while ((c
= generate()) {
16 Xcheck(xs
, xp
); /* expand string if necessary */
17 Xput(xs
, xp
, c
); /* add character */
19 return Xclose(xs
, xp
); /* resize string */
22 * The Xcheck and Xinit macros have a magic + X_EXTRA in the lengths.
23 * This is so that you can put up to X_EXTRA characters in a XString
24 * before calling Xcheck. (See yylex in lex.c)
28 typedef struct XString
{
29 char *end
, *beg
; /* end, begin of string */
30 size_t len
; /* length */
31 Area
*areap
; /* area to allocate/free from */
34 typedef char * XStringP
;
36 /* initialize expandable string */
37 #define Xinit(xs, xp, length, area) do { \
39 (xs).areap = (area); \
40 (xs).beg = alloc((xs).len + X_EXTRA, (xs).areap); \
41 (xs).end = (xs).beg + (xs).len; \
46 /* stuff char into string */
47 #define Xput(xs, xp, c) (*xp++ = (c))
49 /* check if there are at least n bytes left */
50 #define XcheckN(xs, xp, n) do { \
51 int more = ((xp) + (n)) - (xs).end; \
53 xp = Xcheck_grow_(&xs, xp, more); \
56 /* check for overflow, expand string */
57 #define Xcheck(xs, xp) XcheckN(xs, xp, 1)
60 #define Xfree(xs, xp) afree((void*) (xs).beg, (xs).areap)
62 /* close, return string */
63 #define Xclose(xs, xp) (char*) aresize((void*)(xs).beg, \
64 (size_t)((xp) - (xs).beg), (xs).areap)
66 #define Xstring(xs, xp) ((xs).beg)
68 #define Xnleft(xs, xp) ((xs).end - (xp)) /* may be less than 0 */
69 #define Xlength(xs, xp) ((xp) - (xs).beg)
70 #define Xsize(xs, xp) ((xs).end - (xs).beg)
71 #define Xsavepos(xs, xp) ((xp) - (xs).beg)
72 #define Xrestpos(xs, xp, n) ((xs).beg + (n))
74 char * Xcheck_grow_
ARGS((XString
*xsp
, char *xp
, int more
));
77 * expandable vector of generic pointers
80 typedef struct XPtrV
{
81 void **cur
; /* next avail pointer */
82 void **beg
, **end
; /* begin, end of vector */
85 #define XPinit(x, n) do { \
86 register void **vp__; \
87 vp__ = (void**) alloc(sizeofN(void*, n), ATEMP); \
88 (x).cur = (x).beg = vp__; \
92 #define XPput(x, p) do { \
93 if ((x).cur >= (x).end) { \
95 (x).beg = (void**) aresize((void*) (x).beg, \
96 sizeofN(void*, n*2), ATEMP); \
97 (x).cur = (x).beg + n; \
98 (x).end = (x).cur + n; \
103 #define XPptrv(x) ((x).beg)
104 #define XPsize(x) ((x).cur - (x).beg)
106 #define XPclose(x) (void**) aresize((void*)(x).beg, \
107 sizeofN(void*, XPsize(x)), ATEMP)
109 #define XPfree(x) afree((void*) (x).beg, ATEMP)