* NEWS: Mention dfa. * lib/dfa.c: Include xwalloc.h instead of xalloc.h, intprops.h. (position, leaf_set, struct lexer_state, struct parser_state): (struct dfa, mbs_to_wchar, fetch_wc, parse_bracket_exp, struct lexptr): (lex, nsubtoks, copytoks, closure, dfaparse, copy, delete, replace): (state_index, epsclosure, state_separate_contexts, dfaanalyze): (realloc_trans_if_necessary, dfaexec_main, dfaexec_mb): (dfaexec_sb, dfaexec_noop, dfaexec, dfa_supported, dfaoptimize): (dfassbuild, dfacomp, dfafree, icatalloc, enlist, inboth, dfamust): Use ptrdiff_t, not size_t, for sizes intended for walloc. (xpalloc): Remove. All uses replaced by xgrowalloc. (addtok_mb, alloc_position_set, merge_constrained, dfaanalyze) (realloc_trans_if_necessary, build_state, transit_state, dfassbuild): (enlist, comsubs, inboth, allocmust, dfamust, dfaalloc): Use xwalloc.h rather than xalloc.h functions. * lib/dfa.h (dfacomp, dfaexec): Use ptrdiff_t, not size_t, for counts. * modules/dfa (Depends-on): Remove intprops, xalloc, xalloc-die. Add xwalloc. --- ChangeLog | 20 ++++ NEWS | 4 +- lib/dfa.c | 350 +++++++++++++++++++++++++----------------------------------- lib/dfa.h | 4 +- modules/dfa | 4 +- 5 files changed, 170 insertions(+), 212 deletions(-)
diff --git a/ChangeLog b/ChangeLog index d7e6afc..ebd74b5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,25 @@ 2017-06-04 Paul Eggert <[email protected]> + dfa: use xwalloc instead of xalloc + * NEWS: Mention dfa. + * lib/dfa.c: Include xwalloc.h instead of xalloc.h, intprops.h. + (position, leaf_set, struct lexer_state, struct parser_state): + (struct dfa, mbs_to_wchar, fetch_wc, parse_bracket_exp, struct lexptr): + (lex, nsubtoks, copytoks, closure, dfaparse, copy, delete, replace): + (state_index, epsclosure, state_separate_contexts, dfaanalyze): + (realloc_trans_if_necessary, dfaexec_main, dfaexec_mb): + (dfaexec_sb, dfaexec_noop, dfaexec, dfa_supported, dfaoptimize): + (dfassbuild, dfacomp, dfafree, icatalloc, enlist, inboth, dfamust): + Use ptrdiff_t, not size_t, for sizes intended for walloc. + (xpalloc): Remove. All uses replaced by xgrowalloc. + (addtok_mb, alloc_position_set, merge_constrained, dfaanalyze) + (realloc_trans_if_necessary, build_state, transit_state, dfassbuild): + (enlist, comsubs, inboth, allocmust, dfamust, dfaalloc): + Use xwalloc.h rather than xalloc.h functions. + * lib/dfa.h (dfacomp, dfaexec): Use ptrdiff_t, not size_t, for counts. + * modules/dfa (Depends-on): Remove intprops, xalloc, xalloc-die. + Add xwalloc. + careadlinkat: use walloc * NEWS: Mention ptrdiff_t. * lib/careadlinkat.c: Include stdint.h, intprops.h, minmax.h, walloc.h. diff --git a/NEWS b/NEWS index ff60817..1762f40 100644 --- a/NEWS +++ b/NEWS @@ -42,8 +42,8 @@ User visible incompatible changes Date Modules Changes -2017-06-04 careadlinkat careadlinkat now takes - ptrdiff_t instead of size_t arguments for object +2017-06-04 careadlinkat careadlinkat, dfacomp and dfaexec now take + dfa ptrdiff_t instead of size_t arguments for object and byte counts. 2017-05-19 closeout close_stdout longer closes stderr when addresses diff --git a/lib/dfa.c b/lib/dfa.c index 2b9c80e..0e8639c 100644 --- a/lib/dfa.c +++ b/lib/dfa.c @@ -50,9 +50,8 @@ isasciidigit (char c) #include <wchar.h> -#include "intprops.h" -#include "xalloc.h" #include "localeinfo.h" +#include "xwalloc.h" #ifndef FALLTHROUGH # if __GNUC__ < 7 @@ -315,7 +314,7 @@ enum a constraint. */ typedef struct { - size_t index; /* Index into the parse array. */ + ptrdiff_t index; /* Index into the parse array. */ unsigned int constraint; /* Constraint for matching this position. */ } position; @@ -330,8 +329,8 @@ typedef struct /* Sets of leaves are also stored as arrays. */ typedef struct { - size_t *elems; /* Elements of this position set. */ - size_t nelem; /* Number of elements in this set. */ + ptrdiff_t *elems; /* Elements of this position set. */ + ptrdiff_t nelem; /* Number of elements in this set. */ } leaf_set; /* A state of the dfa consists of a set of positions, some flags, @@ -404,9 +403,9 @@ struct regex_syntax struct lexer_state { char const *ptr; /* Pointer to next input character. */ - size_t left; /* Number of characters remaining. */ + ptrdiff_t left; /* Number of characters remaining. */ token lasttok; /* Previous token returned; initially END. */ - size_t parens; /* Count of outstanding left parens. */ + ptrdiff_t parens; /* Count of outstanding left parens. */ int minrep, maxrep; /* Repeat counts for {m,n}. */ /* Wide character representation of the current multibyte character, @@ -429,7 +428,7 @@ struct lexer_state struct parser_state { token tok; /* Lookahead token. */ - size_t depth; /* Current depth of a hypothetical stack + ptrdiff_t depth; /* Current depth of a hypothetical stack holding deferred productions. This is used to determine the depth that will be required of the real stack later on in @@ -446,7 +445,7 @@ struct dfa charclass *charclasses; /* Array of character sets for CSET tokens. */ ptrdiff_t cindex; /* Index for adding new charclasses. */ ptrdiff_t calloc; /* Number of charclasses allocated. */ - size_t canychar; /* Index of anychar class, or (size_t) -1. */ + ptrdiff_t canychar; /* Index of anychar class, or -1. */ /* Scanner state */ struct lexer_state lex; @@ -456,13 +455,13 @@ struct dfa /* Fields filled by the parser. */ token *tokens; /* Postfix parse array. */ - size_t tindex; /* Index for adding new tokens. */ - size_t talloc; /* Number of tokens currently allocated. */ - size_t depth; /* Depth required of an evaluation stack + ptrdiff_t tindex; /* Index for adding new tokens. */ + ptrdiff_t talloc; /* Number of tokens currently allocated. */ + ptrdiff_t depth; /* Depth required of an evaluation stack used for depth-first traversal of the parse tree. */ - size_t nleaves; /* Number of leaves on the parse tree. */ - size_t nregexps; /* Count of parallel regexps being built + ptrdiff_t nleaves; /* Number of leaves on the parse tree. */ + ptrdiff_t nregexps; /* Count of parallel regexps being built with dfaparse. */ bool fast; /* The DFA is fast. */ token utf8_anychar_classes[5]; /* To lower ANYCHAR in UTF-8 locales. */ @@ -556,7 +555,7 @@ struct dfa /* dfaexec implementation. */ char *(*dfaexec) (struct dfa *, char const *, char *, - bool, size_t *, bool *); + bool, ptrdiff_t *, bool *); /* The locale is simple, like the C locale. These locales can be processed more efficiently, as they are single-byte, their native @@ -594,6 +593,7 @@ static void regexp (struct dfa *dfa); This differs from mbrtowc (PWC, S, N, &D->mbs) as follows: + * It uses and returns ptrdiff_t values instead of size_t values. * PWC points to wint_t, not to wchar_t. * The last arg is a dfa *D instead of merely a multibyte conversion state D->mbs. @@ -603,8 +603,8 @@ static void regexp (struct dfa *dfa); * The return value is always in the range 1..N. * D->mbs is always valid afterwards. * *PWC is always set to something. */ -static size_t -mbs_to_wchar (wint_t *pwc, char const *s, size_t n, struct dfa *d) +static ptrdiff_t +mbs_to_wchar (wint_t *pwc, char const *s, ptrdiff_t n, struct dfa *d) { unsigned char uc = s[0]; wint_t wc = d->localeinfo.sbctowc[uc]; @@ -613,7 +613,7 @@ mbs_to_wchar (wint_t *pwc, char const *s, size_t n, struct dfa *d) { wchar_t wch; size_t nbytes = mbrtowc (&wch, s, n, &d->mbs); - if (0 < nbytes && nbytes < (size_t) -2) + if (0 < nbytes && nbytes <= n) { *pwc = wch; return nbytes; @@ -762,66 +762,6 @@ emptyset (charclass const *s) return w == 0; } -/* Grow PA, which points to an array of *NITEMS items, and return the - location of the reallocated array, updating *NITEMS to reflect its - new size. The new array will contain at least NITEMS_INCR_MIN more - items, but will not contain more than NITEMS_MAX items total. - ITEM_SIZE is the size of each item, in bytes. - - ITEM_SIZE and NITEMS_INCR_MIN must be positive. *NITEMS must be - nonnegative. If NITEMS_MAX is -1, it is treated as if it were - infinity. - - If PA is null, then allocate a new array instead of reallocating - the old one. - - Thus, to grow an array A without saving its old contents, do - { free (A); A = xpalloc (NULL, &AITEMS, ...); }. */ - -static void * -xpalloc (void *pa, ptrdiff_t *nitems, ptrdiff_t nitems_incr_min, - ptrdiff_t nitems_max, ptrdiff_t item_size) -{ - ptrdiff_t n0 = *nitems; - - /* The approximate size to use for initial small allocation - requests. This is the largest "small" request for the GNU C - library malloc. */ - enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 }; - - /* If the array is tiny, grow it to about (but no greater than) - DEFAULT_MXFAST bytes. Otherwise, grow it by about 50%. - Adjust the growth according to three constraints: NITEMS_INCR_MIN, - NITEMS_MAX, and what the C language can represent safely. */ - - ptrdiff_t n, nbytes; - if (INT_ADD_WRAPV (n0, n0 >> 1, &n)) - n = PTRDIFF_MAX; - if (0 <= nitems_max && nitems_max < n) - n = nitems_max; - - ptrdiff_t adjusted_nbytes - = ((INT_MULTIPLY_WRAPV (n, item_size, &nbytes) || SIZE_MAX < nbytes) - ? MIN (PTRDIFF_MAX, SIZE_MAX) - : nbytes < DEFAULT_MXFAST ? DEFAULT_MXFAST : 0); - if (adjusted_nbytes) - { - n = adjusted_nbytes / item_size; - nbytes = adjusted_nbytes - adjusted_nbytes % item_size; - } - - if (! pa) - *nitems = 0; - if (n - n0 < nitems_incr_min - && (INT_ADD_WRAPV (n0, nitems_incr_min, &n) - || (0 <= nitems_max && nitems_max < n) - || INT_MULTIPLY_WRAPV (n, item_size, &nbytes))) - xalloc_die (); - pa = xrealloc (pa, nbytes); - *nitems = n; - return pa; -} - /* Ensure that the array addressed by PA holds at least I + 1 items. Either return PA, or reallocate the array and return its new address. Although PA may be null, the returned value is never null. @@ -837,7 +777,7 @@ maybe_realloc (void *pa, ptrdiff_t i, ptrdiff_t *nitems, { if (i < *nitems) return pa; - return xpalloc (pa, nitems, 1, nitems_max, item_size); + return xwgrowalloc (pa, nitems, i - *nitems + 1, nitems_max, item_size); } /* In DFA D, find the index of charclass S, or allocate a new one. */ @@ -940,8 +880,8 @@ using_simple_locale (bool multibyte) static int fetch_wc (struct dfa *dfa) { - size_t nbytes = mbs_to_wchar (&dfa->lex.wctok, dfa->lex.ptr, dfa->lex.left, - dfa); + ptrdiff_t nbytes = mbs_to_wchar (&dfa->lex.wctok, dfa->lex.ptr, + dfa->lex.left, dfa); dfa->lex.cur_mb_len = nbytes; int c = nbytes == 1 ? to_uchar (dfa->lex.ptr[0]) : EOF; dfa->lex.ptr += nbytes; @@ -1046,7 +986,7 @@ parse_bracket_exp (struct dfa *dfa) { enum { MAX_BRACKET_STRING_LEN = 32 }; char str[MAX_BRACKET_STRING_LEN + 1]; - size_t len = 0; + ptrdiff_t len = 0; for (;;) { c = bracket_fetch_wc (dfa); @@ -1227,7 +1167,7 @@ parse_bracket_exp (struct dfa *dfa) struct lexptr { char const *ptr; - size_t left; + ptrdiff_t left; }; static void @@ -1475,7 +1415,7 @@ lex (struct dfa *dfa) case '.': if (backslash) goto normal_char; - if (dfa->canychar == (size_t) -1) + if (dfa->canychar < 0) { charclass ccl; fillset (&ccl); @@ -1598,11 +1538,11 @@ addtok_mb (struct dfa *dfa, token t, char mbprop) { if (dfa->talloc == dfa->tindex) { - dfa->tokens = x2nrealloc (dfa->tokens, &dfa->talloc, - sizeof *dfa->tokens); + dfa->tokens = xwgrowalloc (dfa->tokens, &dfa->talloc, 1, -1, + sizeof *dfa->tokens); if (dfa->localeinfo.multibyte) - dfa->multibyte_prop = xnrealloc (dfa->multibyte_prop, dfa->talloc, - sizeof *dfa->multibyte_prop); + dfa->multibyte_prop = xwreallocarray (dfa->multibyte_prop, dfa->talloc, + sizeof *dfa->multibyte_prop); } if (dfa->localeinfo.multibyte) dfa->multibyte_prop[dfa->tindex] = mbprop; @@ -1855,8 +1795,8 @@ atom (struct dfa *dfa) } /* Return the number of tokens in the given subexpression. */ -static size_t _GL_ATTRIBUTE_PURE -nsubtoks (struct dfa const *dfa, size_t tindex) +static ptrdiff_t _GL_ATTRIBUTE_PURE +nsubtoks (struct dfa const *dfa, ptrdiff_t tindex) { switch (dfa->tokens[tindex - 1]) { @@ -1869,7 +1809,7 @@ nsubtoks (struct dfa const *dfa, size_t tindex) case CAT: case OR: { - size_t ntoks1 = nsubtoks (dfa, tindex - 1); + ptrdiff_t ntoks1 = nsubtoks (dfa, tindex - 1); return 1 + ntoks1 + nsubtoks (dfa, tindex - 1 - ntoks1); } } @@ -1877,14 +1817,14 @@ nsubtoks (struct dfa const *dfa, size_t tindex) /* Copy the given subexpression to the top of the tree. */ static void -copytoks (struct dfa *dfa, size_t tindex, size_t ntokens) +copytoks (struct dfa *dfa, ptrdiff_t tindex, ptrdiff_t ntokens) { if (dfa->localeinfo.multibyte) - for (size_t i = 0; i < ntokens; ++i) + for (ptrdiff_t i = 0; i < ntokens; ++i) addtok_mb (dfa, dfa->tokens[tindex + i], dfa->multibyte_prop[tindex + i]); else - for (size_t i = 0; i < ntokens; ++i) + for (ptrdiff_t i = 0; i < ntokens; ++i) addtok_mb (dfa, dfa->tokens[tindex + i], 3); } @@ -1896,8 +1836,8 @@ closure (struct dfa *dfa) || dfa->parse.tok == PLUS || dfa->parse.tok == REPMN) if (dfa->parse.tok == REPMN && (dfa->lex.minrep || dfa->lex.maxrep)) { - size_t ntokens = nsubtoks (dfa, dfa->tindex); - size_t tindex = dfa->tindex - ntokens; + ptrdiff_t ntokens = nsubtoks (dfa, dfa->tindex); + ptrdiff_t tindex = dfa->tindex - ntokens; if (dfa->lex.maxrep < 0) addtok (dfa, PLUS); if (dfa->lex.minrep == 0) @@ -1957,7 +1897,7 @@ regexp (struct dfa *dfa) length of the string, so s can include NUL characters. D is a pointer to the struct dfa to parse into. */ static void -dfaparse (char const *s, size_t len, struct dfa *d) +dfaparse (char const *s, ptrdiff_t len, struct dfa *d) { d->lex.ptr = s; d->lex.left = len; @@ -1993,8 +1933,8 @@ copy (position_set const *src, position_set *dst) if (dst->alloc < src->nelem) { free (dst->elems); - dst->elems = xpalloc (NULL, &dst->alloc, src->nelem - dst->alloc, -1, - sizeof *dst->elems); + dst->elems = xwgrowalloc (NULL, &dst->alloc, src->nelem - dst->alloc, -1, + sizeof *dst->elems); } dst->nelem = src->nelem; if (src->nelem != 0) @@ -2002,9 +1942,9 @@ copy (position_set const *src, position_set *dst) } static void -alloc_position_set (position_set *s, size_t size) +alloc_position_set (position_set *s, ptrdiff_t size) { - s->elems = xnmalloc (size, sizeof *s->elems); + s->elems = xwnmalloc (size, sizeof *s->elems); s->alloc = size; s->nelem = 0; } @@ -2052,7 +1992,7 @@ merge_constrained (position_set const *s1, position_set const *s2, { free (m->elems); m->alloc = s1->nelem; - m->elems = xpalloc (NULL, &m->alloc, s2->nelem, -1, sizeof *m->elems); + m->elems = xwgrowalloc (NULL, &m->alloc, s2->nelem, -1, sizeof *m->elems); } m->nelem = 0; while (i < s1->nelem || j < s2->nelem) @@ -2088,19 +2028,19 @@ merge (position_set const *s1, position_set const *s2, position_set *m) /* Delete a position from a set. Return the nonzero constraint of the deleted position, or zero if there was no such position. */ static unsigned int -delete (size_t del, position_set *s) +delete (ptrdiff_t del, position_set *s) { - size_t count = s->nelem; - size_t lo = 0, hi = count; + ptrdiff_t count = s->nelem; + ptrdiff_t lo = 0, hi = count; while (lo < hi) { - size_t mid = (lo + hi) >> 1; + ptrdiff_t mid = (lo + hi) >> 1; if (s->elems[mid].index > del) lo = mid + 1; else if (s->elems[mid].index == del) { unsigned int c = s->elems[mid].constraint; - size_t i; + ptrdiff_t i; for (i = mid; i + 1 < count; i++) s->elems[i] = s->elems[i + 1]; s->nelem = i; @@ -2114,7 +2054,7 @@ delete (size_t del, position_set *s) /* Replace a position with the followed set. */ static void -replace (position_set *dst, size_t del, position_set *add, +replace (position_set *dst, ptrdiff_t del, position_set *add, unsigned int constraint, position_set *tmp) { unsigned int c = delete (del, dst) & constraint; @@ -2156,10 +2096,10 @@ state_index (struct dfa *d, position_set const *s, int context) } #ifdef DEBUG - fprintf (stderr, "new state %zd\n nextpos:", i); + fprintf (stderr, "new state %td\n nextpos:", i); for (state_num j = 0; j < s->nelem; j++) { - fprintf (stderr, " %zu:", s->elems[j].index); + fprintf (stderr, " %td:", s->elems[j].index); prtok (d->tokens[s->elems[j].index]); } fprintf (stderr, "\n context:"); @@ -2220,7 +2160,7 @@ epsclosure (position_set *initial, struct dfa const *d) { position_set tmp; alloc_position_set (&tmp, d->nleaves); - for (size_t i = 0; i < d->tindex; ++i) + for (ptrdiff_t i = 0; i < d->tindex; ++i) if (d->follows[i].nelem > 0 && d->tokens[i] >= NOTCHAR && d->tokens[i] != BACKREF && d->tokens[i] != ANYCHAR && d->tokens[i] != MBCSET && d->tokens[i] < CSET) @@ -2253,7 +2193,7 @@ epsclosure (position_set *initial, struct dfa const *d) delete (i, &d->follows[i]); - for (size_t j = 0; j < d->tindex; j++) + for (ptrdiff_t j = 0; j < d->tindex; j++) if (i != j && d->follows[j].nelem > 0) replace (&d->follows[j], i, &d->follows[i], constraint, &tmp); @@ -2294,7 +2234,7 @@ state_separate_contexts (position_set const *s) { int separate_contexts = 0; - for (size_t j = 0; j < s->nelem; j++) + for (ptrdiff_t j = 0; j < s->nelem; j++) { if (prev_newline_dependent (s->elems[j].constraint)) separate_contexts |= CTX_NEWLINE; @@ -2362,7 +2302,7 @@ static void dfaanalyze (struct dfa *d, bool searchflag) { /* Array allocated to hold position sets. */ - position *posalloc = xnmalloc (d->nleaves, 2 * sizeof *posalloc); + position *posalloc = xwnmalloc (d->nleaves, 2 * sizeof *posalloc); /* Firstpos and lastpos elements. */ position *firstpos = posalloc + d->nleaves; position *lastpos = firstpos + d->nleaves; @@ -2374,17 +2314,17 @@ dfaanalyze (struct dfa *d, bool searchflag) bool nullable; /* Counts of firstpos and lastpos sets. */ - size_t nfirstpos; - size_t nlastpos; - } *stkalloc = xnmalloc (d->depth, sizeof *stkalloc), *stk = stkalloc; + ptrdiff_t nfirstpos; + ptrdiff_t nlastpos; + } *stkalloc = xwnmalloc (d->depth, sizeof *stkalloc), *stk = stkalloc; position_set merged; /* Result of merging sets. */ #ifdef DEBUG fprintf (stderr, "dfaanalyze:\n"); - for (size_t i = 0; i < d->tindex; ++i) + for (ptrdiff_t i = 0; i < d->tindex; ++i) { - fprintf (stderr, " %zu:", i); + fprintf (stderr, " %td:", i); prtok (d->tokens[i]); } putc ('\n', stderr); @@ -2392,9 +2332,9 @@ dfaanalyze (struct dfa *d, bool searchflag) d->searchflag = searchflag; alloc_position_set (&merged, d->nleaves); - d->follows = xcalloc (d->tindex, sizeof *d->follows); + d->follows = xwcalloc (d->tindex, sizeof *d->follows); - for (size_t i = 0; i < d->tindex; ++i) + for (ptrdiff_t i = 0; i < d->tindex; ++i) { switch (d->tokens[i]) { @@ -2416,7 +2356,7 @@ dfaanalyze (struct dfa *d, bool searchflag) tmp.nelem = stk[-1].nfirstpos; tmp.elems = firstpos; position *pos = lastpos; - for (size_t j = 0; j < stk[-1].nlastpos; j++) + for (ptrdiff_t j = 0; j < stk[-1].nlastpos; j++) { merge (&tmp, &d->follows[pos[j].index], &merged); copy (&merged, &d->follows[pos[j].index]); @@ -2437,7 +2377,7 @@ dfaanalyze (struct dfa *d, bool searchflag) tmp.nelem = stk[-1].nfirstpos; tmp.elems = firstpos; position *pos = lastpos + stk[-1].nlastpos; - for (size_t j = 0; j < stk[-2].nlastpos; j++) + for (ptrdiff_t j = 0; j < stk[-2].nlastpos; j++) { merge (&tmp, &d->follows[pos[j].index], &merged); copy (&merged, &d->follows[pos[j].index]); @@ -2458,7 +2398,7 @@ dfaanalyze (struct dfa *d, bool searchflag) else { position *pos = lastpos + stk[-2].nlastpos; - for (size_t j = stk[-1].nlastpos; j-- > 0;) + for (ptrdiff_t j = stk[-1].nlastpos; j-- > 0;) pos[j] = lastpos[j]; lastpos += stk[-2].nlastpos; stk[-2].nlastpos = stk[-1].nlastpos; @@ -2501,21 +2441,21 @@ dfaanalyze (struct dfa *d, bool searchflag) } #ifdef DEBUG /* ... balance the above nonsyntactic #ifdef goo... */ - fprintf (stderr, "node %zu:", i); + fprintf (stderr, "node %td:", i); prtok (d->tokens[i]); putc ('\n', stderr); fprintf (stderr, stk[-1].nullable ? " nullable: yes\n" : " nullable: no\n"); fprintf (stderr, " firstpos:"); - for (size_t j = stk[-1].nfirstpos; j-- > 0;) + for (ptrdiff_t j = stk[-1].nfirstpos; j-- > 0;) { - fprintf (stderr, " %zu:", firstpos[j].index); + fprintf (stderr, " %td:", firstpos[j].index); prtok (d->tokens[firstpos[j].index]); } fprintf (stderr, "\n lastpos:"); - for (size_t j = stk[-1].nlastpos; j-- > 0;) + for (ptrdiff_t j = stk[-1].nlastpos; j-- > 0;) { - fprintf (stderr, " %zu:", lastpos[j].index); + fprintf (stderr, " %td:", lastpos[j].index); prtok (d->tokens[lastpos[j].index]); } putc ('\n', stderr); @@ -2523,17 +2463,17 @@ dfaanalyze (struct dfa *d, bool searchflag) } #ifdef DEBUG - for (size_t i = 0; i < d->tindex; ++i) + for (ptrdiff_t i = 0; i < d->tindex; ++i) if (d->tokens[i] < NOTCHAR || d->tokens[i] == BACKREF || d->tokens[i] == ANYCHAR || d->tokens[i] == MBCSET || d->tokens[i] >= CSET) { - fprintf (stderr, "follows(%zu:", i); + fprintf (stderr, "follows(%td:", i); prtok (d->tokens[i]); fprintf (stderr, "):"); - for (size_t j = d->follows[i].nelem; j-- > 0;) + for (ptrdiff_t j = d->follows[i].nelem; j-- > 0;) { - fprintf (stderr, " %zu:", d->follows[i].elems[j].index); + fprintf (stderr, " %td:", d->follows[i].elems[j].index); prtok (d->tokens[d->follows[i].elems[j].index]); } putc ('\n', stderr); @@ -2543,7 +2483,7 @@ dfaanalyze (struct dfa *d, bool searchflag) /* Get the epsilon closure of the firstpos of the regexp. The result will be the set of positions of state 0. */ merged.nelem = 0; - for (size_t i = 0; i < stk[-1].nfirstpos; ++i) + for (ptrdiff_t i = 0; i < stk[-1].nfirstpos; ++i) insert (firstpos[i], &merged); /* For each follow set that is the follow set of a real position, replace @@ -2577,18 +2517,18 @@ realloc_trans_if_necessary (struct dfa *d) { state_num **realtrans = d->trans ? d->trans - 2 : NULL; ptrdiff_t newalloc1 = realtrans ? d->tralloc + 2 : 0; - realtrans = xpalloc (realtrans, &newalloc1, d->sindex - oldalloc, - -1, sizeof *realtrans); + realtrans = xwgrowalloc (realtrans, &newalloc1, d->sindex - oldalloc, + -1, sizeof *realtrans); realtrans[0] = realtrans[1] = NULL; d->trans = realtrans + 2; ptrdiff_t newalloc = d->tralloc = newalloc1 - 2; - d->fails = xnrealloc (d->fails, newalloc, sizeof *d->fails); - d->success = xnrealloc (d->success, newalloc, sizeof *d->success); - d->newlines = xnrealloc (d->newlines, newalloc, sizeof *d->newlines); + d->fails = xwreallocarray (d->fails, newalloc, sizeof *d->fails); + d->success = xwreallocarray (d->success, newalloc, sizeof *d->success); + d->newlines = xwreallocarray (d->newlines, newalloc, sizeof *d->newlines); if (d->localeinfo.multibyte) { realtrans = d->mb_trans ? d->mb_trans - 2 : NULL; - realtrans = xnrealloc (realtrans, newalloc1, sizeof *realtrans); + realtrans = xwreallocarray (realtrans, newalloc1, sizeof *realtrans); if (oldalloc == 0) realtrans[0] = realtrans[1] = NULL; d->mb_trans = realtrans + 2; @@ -2671,7 +2611,7 @@ build_state (state_num s, struct dfa *d, unsigned char uc) } d->trcount++; - *ptrans = trans = xmalloc (NOTCHAR * sizeof *trans); + *ptrans = trans = xwmalloc (NOTCHAR * sizeof *trans); /* Fill transition table with a default value which means that the transited state has not been calculated yet. */ @@ -2690,14 +2630,14 @@ build_state (state_num s, struct dfa *d, unsigned char uc) /* Positions that match the input char. */ leaf_set group; - group.elems = xnmalloc (d->nleaves, sizeof *group.elems); + group.elems = xwnmalloc (d->nleaves, sizeof *group.elems); group.nelem = 0; /* The group's label. */ charclass label; fillset (&label); - for (size_t i = 0; i < d->states[s].elems.nelem; ++i) + for (ptrdiff_t i = 0; i < d->states[s].elems.nelem; ++i) { charclass matches; /* Set of matching characters. */ position pos = d->states[s].elems.elems[i]; @@ -2733,7 +2673,7 @@ build_state (state_num s, struct dfa *d, unsigned char uc) if (d->states[s].mbps.nelem == 0) alloc_position_set (&d->states[s].mbps, d->follows[pos.index].nelem); - for (size_t j = 0; j < d->follows[pos.index].nelem; j++) + for (ptrdiff_t j = 0; j < d->follows[pos.index].nelem; j++) insert (d->follows[pos.index].elems[j], &d->states[s].mbps); } } @@ -2746,15 +2686,15 @@ build_state (state_num s, struct dfa *d, unsigned char uc) { if (!succeeds_in_context (pos.constraint, d->states[s].context, CTX_NEWLINE)) - for (size_t j = 0; j < CHARCLASS_WORDS; ++j) + for (ptrdiff_t j = 0; j < CHARCLASS_WORDS; ++j) matches.w[j] &= ~d->syntax.newline.w[j]; if (!succeeds_in_context (pos.constraint, d->states[s].context, CTX_LETTER)) - for (size_t j = 0; j < CHARCLASS_WORDS; ++j) + for (ptrdiff_t j = 0; j < CHARCLASS_WORDS; ++j) matches.w[j] &= ~d->syntax.letters.w[j]; if (!succeeds_in_context (pos.constraint, d->states[s].context, CTX_NONE)) - for (size_t j = 0; j < CHARCLASS_WORDS; ++j) + for (ptrdiff_t j = 0; j < CHARCLASS_WORDS; ++j) matches.w[j] &= d->syntax.letters.w[j] | d->syntax.newline.w[j]; /* If there are no characters left, there's no point in going on. */ @@ -2769,10 +2709,10 @@ build_state (state_num s, struct dfa *d, unsigned char uc) } #ifdef DEBUG - fprintf (stderr, " nextpos %zu:", pos.index); + fprintf (stderr, " nextpos %td:", pos.index); prtok (d->tokens[pos.index]); fprintf (stderr, " of"); - for (size_t j = 0; j < NOTCHAR; j++) + for (ptrdiff_t j = 0; j < NOTCHAR; j++) if (tstbit (j, &matches)) fprintf (stderr, " 0x%02zx", j); fprintf (stderr, "\n"); @@ -2780,13 +2720,13 @@ build_state (state_num s, struct dfa *d, unsigned char uc) if (matched) { - for (size_t k = 0; k < CHARCLASS_WORDS; ++k) + for (ptrdiff_t k = 0; k < CHARCLASS_WORDS; ++k) label.w[k] &= matches.w[k]; group.elems[group.nelem++] = pos.index; } else { - for (size_t k = 0; k < CHARCLASS_WORDS; ++k) + for (ptrdiff_t k = 0; k < CHARCLASS_WORDS; ++k) label.w[k] &= ~matches.w[k]; } } @@ -2800,8 +2740,8 @@ build_state (state_num s, struct dfa *d, unsigned char uc) /* Find the union of the follows of the positions of the group. This is a hideously inefficient loop. Fix it someday. */ - for (size_t j = 0; j < group.nelem; ++j) - for (size_t k = 0; k < d->follows[group.elems[j]].nelem; ++k) + for (ptrdiff_t j = 0; j < group.nelem; ++j) + for (ptrdiff_t k = 0; k < d->follows[group.elems[j]].nelem; ++k) insert (d->follows[group.elems[j]].elems[k], &follows); /* If we are building a searching matcher, throw in the positions @@ -2829,7 +2769,7 @@ build_state (state_num s, struct dfa *d, unsigned char uc) if (!mergeit) { mergeit = true; - for (size_t j = 0; mergeit && j < follows.nelem; j++) + for (ptrdiff_t j = 0; mergeit && j < follows.nelem; j++) mergeit &= d->multibyte_prop[follows.elems[j].index]; } if (mergeit) @@ -2880,7 +2820,7 @@ build_state (state_num s, struct dfa *d, unsigned char uc) } /* Set the transitions for each character in the label. */ - for (size_t i = 0; i < NOTCHAR; i++) + for (ptrdiff_t i = 0; i < NOTCHAR; i++) if (tstbit (i, &label)) switch (d->syntax.sbit[i]) { @@ -2897,7 +2837,7 @@ build_state (state_num s, struct dfa *d, unsigned char uc) #ifdef DEBUG fprintf (stderr, "trans table %td", s); - for (size_t i = 0; i < NOTCHAR; ++i) + for (ptrdiff_t i = 0; i < NOTCHAR; ++i) { if (!(i & 0xf)) fprintf (stderr, "\n"); @@ -3007,7 +2947,7 @@ transit_state (struct dfa *d, state_num s, unsigned char const **pp, { enum { TRANSPTR_SIZE = sizeof *d->mb_trans[s] }; enum { TRANSALLOC_SIZE = MAX_TRCOUNT * TRANSPTR_SIZE }; - d->mb_trans[s] = xmalloc (TRANSALLOC_SIZE); + d->mb_trans[s] = xwmalloc (TRANSALLOC_SIZE); for (int i = 0; i < MAX_TRCOUNT; i++) d->mb_trans[s][i] = -1; } @@ -3080,7 +3020,7 @@ skip_remains_mb (struct dfa *d, unsigned char const *p, static inline char * dfaexec_main (struct dfa *d, char const *begin, char *end, bool allow_nl, - size_t *count, bool multibyte) + ptrdiff_t *count, bool multibyte) { if (MAX_TRCOUNT <= d->sindex) { @@ -3138,7 +3078,7 @@ dfaexec_main (struct dfa *d, char const *begin, char *end, bool allow_nl, alloc_position_set (&d->mb_follows, d->nleaves); } - size_t nlcount = 0; + ptrdiff_t nlcount = 0; for (;;) { state_num *t; @@ -3263,14 +3203,14 @@ dfaexec_main (struct dfa *d, char const *begin, char *end, bool allow_nl, static char * dfaexec_mb (struct dfa *d, char const *begin, char *end, - bool allow_nl, size_t *count, bool *backref) + bool allow_nl, ptrdiff_t *count, bool *backref) { return dfaexec_main (d, begin, end, allow_nl, count, true); } static char * dfaexec_sb (struct dfa *d, char const *begin, char *end, - bool allow_nl, size_t *count, bool *backref) + bool allow_nl, ptrdiff_t *count, bool *backref) { return dfaexec_main (d, begin, end, allow_nl, count, false); } @@ -3279,7 +3219,7 @@ dfaexec_sb (struct dfa *d, char const *begin, char *end, any regexp that uses a construct not supported by this code. */ static char * dfaexec_noop (struct dfa *d, char const *begin, char *end, - bool allow_nl, size_t *count, bool *backref) + bool allow_nl, ptrdiff_t *count, bool *backref) { *backref = true; return (char *) begin; @@ -3291,7 +3231,7 @@ dfaexec_noop (struct dfa *d, char const *begin, char *end, char * dfaexec (struct dfa *d, char const *begin, char *end, - bool allow_nl, size_t *count, bool *backref) + bool allow_nl, ptrdiff_t *count, bool *backref) { return d->dfaexec (d, begin, end, allow_nl, count, backref); } @@ -3328,7 +3268,7 @@ free_mbdata (struct dfa *d) static bool _GL_ATTRIBUTE_PURE dfa_supported (struct dfa const *d) { - for (size_t i = 0; i < d->tindex; i++) + for (ptrdiff_t i = 0; i < d->tindex; i++) { switch (d->tokens[i]) { @@ -3354,7 +3294,7 @@ dfaoptimize (struct dfa *d) return; bool have_backref = false; - for (size_t i = 0; i < d->tindex; ++i) + for (ptrdiff_t i = 0; i < d->tindex; ++i) { switch (d->tokens[i]) { @@ -3405,20 +3345,20 @@ dfassbuild (struct dfa *d) sup->success = NULL; sup->newlines = NULL; - sup->charclasses = xnmalloc (sup->calloc, sizeof *sup->charclasses); + sup->charclasses = xwnmalloc (sup->calloc, sizeof *sup->charclasses); if (d->cindex) { memcpy (sup->charclasses, d->charclasses, d->cindex * sizeof *sup->charclasses); } - sup->tokens = xnmalloc (d->tindex, 2 * sizeof *sup->tokens); + sup->tokens = xwnmalloc (d->tindex, 2 * sizeof *sup->tokens); sup->talloc = d->tindex * 2; bool have_achar = false; bool have_nchar = false; - size_t j; - for (size_t i = j = 0; i < d->tindex; i++) + ptrdiff_t j; + for (ptrdiff_t i = j = 0; i < d->tindex; i++) { switch (d->tokens[i]) { @@ -3469,7 +3409,7 @@ dfassbuild (struct dfa *d) /* Parse and analyze a single string of the given length. */ void -dfacomp (char const *s, size_t len, struct dfa *d, bool searchflag) +dfacomp (char const *s, ptrdiff_t len, struct dfa *d, bool searchflag) { dfaparse (s, len, d); dfassbuild (d); @@ -3501,7 +3441,7 @@ dfafree (struct dfa *d) if (d->localeinfo.multibyte) free_mbdata (d); - for (size_t i = 0; i < d->sindex; ++i) + for (ptrdiff_t i = 0; i < d->sindex; ++i) { free (d->states[i].elems.elems); free (d->states[i].mbps.elems); @@ -3510,14 +3450,14 @@ dfafree (struct dfa *d) if (d->follows) { - for (size_t i = 0; i < d->tindex; ++i) + for (ptrdiff_t i = 0; i < d->tindex; ++i) free (d->follows[i].elems); free (d->follows); } if (d->trans) { - for (size_t i = 0; i < d->tralloc; ++i) + for (ptrdiff_t i = 0; i < d->tralloc; ++i) { free (d->trans[i]); free (d->fails[i]); @@ -3618,11 +3558,11 @@ dfafree (struct dfa *d) static char * icatalloc (char *old, char const *new) { - size_t newsize = strlen (new); + ptrdiff_t newsize = strlen (new); if (newsize == 0) return old; - size_t oldsize = strlen (old); - char *result = xrealloc (old, oldsize + newsize + 1); + ptrdiff_t oldsize = strlen (old); + char *result = xwrealloc (old, oldsize + newsize + 1); memcpy (result + oldsize, new, newsize + 1); return result; } @@ -3635,12 +3575,12 @@ freelist (char **cpp) } static char ** -enlist (char **cpp, char *new, size_t len) +enlist (char **cpp, char *new, ptrdiff_t len) { - new = memcpy (xmalloc (len + 1), new, len); + new = memcpy (xwnmalloc (len + 1, 1), new, len); new[len] = '\0'; /* Is there already something in the list that's new (or longer)? */ - size_t i; + ptrdiff_t i; for (i = 0; cpp[i] != NULL; ++i) if (strstr (cpp[i], new) != NULL) { @@ -3648,7 +3588,7 @@ enlist (char **cpp, char *new, size_t len) return cpp; } /* Eliminate any obsoleted strings. */ - for (size_t j = 0; cpp[j] != NULL; ) + for (ptrdiff_t j = 0; cpp[j] != NULL; ) if (strstr (new, cpp[j]) == NULL) ++j; else @@ -3660,7 +3600,7 @@ enlist (char **cpp, char *new, size_t len) cpp[i] = NULL; } /* Add the new string. */ - cpp = xnrealloc (cpp, i + 2, sizeof *cpp); + cpp = xwreallocarray (cpp, i + 2, sizeof *cpp); cpp[i] = new; cpp[i + 1] = NULL; return cpp; @@ -3671,15 +3611,15 @@ enlist (char **cpp, char *new, size_t len) static char ** comsubs (char *left, char const *right) { - char **cpp = xzalloc (sizeof *cpp); + char **cpp = xwzalloc (sizeof *cpp); for (char *lcp = left; *lcp != '\0'; lcp++) { - size_t len = 0; + ptrdiff_t len = 0; char *rcp = strchr (right, *lcp); while (rcp != NULL) { - size_t i; + ptrdiff_t i; for (i = 1; lcp[i] != '\0' && lcp[i] == rcp[i]; ++i) continue; if (i > len) @@ -3705,11 +3645,11 @@ addlists (char **old, char **new) static char ** inboth (char **left, char **right) { - char **both = xzalloc (sizeof *both); + char **both = xwzalloc (sizeof *both); - for (size_t lnum = 0; left[lnum] != NULL; ++lnum) + for (ptrdiff_t lnum = 0; left[lnum] != NULL; ++lnum) { - for (size_t rnum = 0; right[rnum] != NULL; ++rnum) + for (ptrdiff_t rnum = 0; right[rnum] != NULL; ++rnum) { char **temp = comsubs (left[lnum], right[rnum]); both = addlists (both, temp); @@ -3734,13 +3674,13 @@ struct must }; static must * -allocmust (must *mp, size_t size) +allocmust (must *mp, ptrdiff_t size) { - must *new_mp = xmalloc (sizeof *new_mp); - new_mp->in = xzalloc (sizeof *new_mp->in); - new_mp->left = xzalloc (size); - new_mp->right = xzalloc (size); - new_mp->is = xzalloc (size); + must *new_mp = xwmalloc (sizeof *new_mp); + new_mp->in = xwzalloc (sizeof *new_mp->in); + new_mp->left = xwzalloc (size); + new_mp->right = xwzalloc (size); + new_mp->is = xwzalloc (size); new_mp->begline = false; new_mp->endline = false; new_mp->prev = mp; @@ -3780,7 +3720,7 @@ dfamust (struct dfa const *d) bool need_endline = false; bool case_fold_unibyte = d->syntax.case_fold && MB_CUR_MAX == 1; - for (size_t ri = 0; ri < d->tindex; ++ri) + for (ptrdiff_t ri = 0; ri < d->tindex; ++ri) { token t = d->tokens[ri]; switch (t) @@ -3820,7 +3760,7 @@ dfamust (struct dfa const *d) char **new; must *rmp = mp; must *lmp = mp = mp->prev; - size_t j, ln, rn, n; + ptrdiff_t j, ln, rn, n; /* Guaranteed to be. Unlikely, but ... */ if (streq (lmp->is, rmp->is)) @@ -3835,7 +3775,7 @@ dfamust (struct dfa const *d) lmp->endline = false; } /* Left side--easy */ - size_t i = 0; + ptrdiff_t i = 0; while (lmp->left[i] != '\0' && lmp->left[i] == rmp->left[i]) ++i; lmp->left[i] = '\0'; @@ -3865,7 +3805,7 @@ dfamust (struct dfa const *d) case END: assert (!mp->prev); - for (size_t i = 0; mp->in[i] != NULL; ++i) + for (ptrdiff_t i = 0; mp->in[i] != NULL; ++i) if (strlen (mp->in[i]) > strlen (result)) result = mp->in[i]; if (streq (result, mp->is)) @@ -3889,9 +3829,9 @@ dfamust (struct dfa const *d) lmp->in = addlists (lmp->in, rmp->in); if (lmp->right[0] != '\0' && rmp->left[0] != '\0') { - size_t lrlen = strlen (lmp->right); - size_t rllen = strlen (rmp->left); - char *tp = xmalloc (lrlen + rllen); + ptrdiff_t lrlen = strlen (lmp->right); + ptrdiff_t rllen = strlen (rmp->left); + char *tp = xwmalloc (lrlen + rllen); memcpy (tp, lmp->right, lrlen); memcpy (tp + lrlen, rmp->left, rllen); lmp->in = enlist (lmp->in, tp, lrlen + rllen); @@ -3955,7 +3895,7 @@ dfamust (struct dfa const *d) } } - size_t rj = ri + 2; + ptrdiff_t rj = ri + 2; if (d->tokens[ri + 1] == CAT) { for (; rj < d->tindex - 1; rj += 2) @@ -3970,7 +3910,7 @@ dfamust (struct dfa const *d) mp->is[0] = mp->left[0] = mp->right[0] = case_fold_unibyte ? toupper (t) : t; - size_t i; + ptrdiff_t i; for (i = 1; ri + 2 < rj; i++) { ri += 2; @@ -3988,11 +3928,11 @@ dfamust (struct dfa const *d) struct dfamust *dm = NULL; if (*result) { - dm = xmalloc (sizeof *dm); + dm = xwmalloc (sizeof *dm); dm->exact = exact; dm->begline = begline; dm->endline = endline; - dm->must = xstrdup (result); + dm->must = xwstrdup (result); } while (mp) @@ -4015,7 +3955,7 @@ dfamustfree (struct dfamust *dm) struct dfa * dfaalloc (void) { - return xmalloc (sizeof (struct dfa)); + return xwmalloc (sizeof (struct dfa)); } /* Initialize DFA. */ diff --git a/lib/dfa.h b/lib/dfa.h index a8d514b..1a50b4b 100644 --- a/lib/dfa.h +++ b/lib/dfa.h @@ -80,7 +80,7 @@ extern void dfamustfree (struct dfamust *); /* Compile the given string of the given length into the given struct dfa. Final argument is a flag specifying whether to build a searching or an exact matcher. */ -extern void dfacomp (char const *, size_t, struct dfa *, bool); +extern void dfacomp (char const *, ptrdiff_t, struct dfa *, bool); /* Search through a buffer looking for a match to the given struct dfa. Find the first occurrence of a string matching the regexp in the @@ -95,7 +95,7 @@ extern void dfacomp (char const *, size_t, struct dfa *, bool); encountered a back-reference. The caller can use this to decide whether to fall back on a backtracking matcher. */ extern char *dfaexec (struct dfa *d, char const *begin, char *end, - bool allow_nl, size_t *count, bool *backref); + bool allow_nl, ptrdiff_t *count, bool *backref); /* Return a superset for D. The superset matches everything that D matches, along with some other strings (though the latter should be diff --git a/modules/dfa b/modules/dfa index 504e7f0..fb4212a 100644 --- a/modules/dfa +++ b/modules/dfa @@ -11,7 +11,6 @@ Depends-on: assert c99 ctype -intprops isblank locale regex @@ -24,8 +23,7 @@ string verify wchar wctype-h -xalloc -xalloc-die +xwalloc configure.ac: -- 2.9.4
